Removing the double quotes from a string - vb.net

I have a variable images that is a String. The value of the images are in this format:
"['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']"
How can I convert the value of the images to something without the double quotes in the beginning and ending (or should I change the String into some other types of variables? The format that I want is this:
['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']
No double quotes
Thanks

The String class has a Replace method that will do that.
Dim clean as String
clean = images.Replace("""", "")

Try this...
Dim s as String = "['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']"
s = s.Replace("'", "").Trim()
Any character or word or phrase or even a sentence is considered a string when it's enclosed in double quotes, but if the value of your string literally has double quotes, like this "SAMPLE", and you want to remove the double quotes, then do something...
Dim s as String = ""['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']""
s = s.Replace("""", "").Trim()
Yes I noticed...double double quotes...I equated s to something that you say is passed from MATLAB, the string literally has double quotes, so to remove this, you replace double double quotes with nothing. That's how you do it in .NET. Your compiler interprets double double quotes as just a single quote :)

This worked for me: (Line had a double quote that I didn't want)
Line = Line.Replace(ChrW(34), "").Trim()

Based on the OP's comments I think there is some confusion. Maybe you are new to .Net, coming from MATLAB.
To re-iterate in VB or C# .Net this is an Empty string:
""
that is a string with length == 0. That is something distinct from a string with the value of a quote or double quote.
To continue:
Dim singleQuoteString As String = "'"
Dim doubleQuoteString As String = """"
and so, what you are asking for
['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']
is not a string.
Now, if you want a Textbox, , console output, or something else to show that value then just take your string, like
Dim listOfImagesAsAString = "['C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB01.JPG';'C:\Users\Elvin Gentiles\Desktop\RiceLAB\BLB02.JPG']"
and assign it to where you want it, like
Console.Write(listOfImagesAsAString)
then the command prompt will show what you are asking for.

Related

How to split on a string instead of a character?

I have a file name like below:
sub_fa__hotchkis_type1a__180310__PUO4x4__180813
I want to separate it with double underscores "__" and using this code:
Dim MdlNameArr() As String = Path.GetFileNameWithoutExtension(strProjMdlName).Split(New Char() {"__"}, StringSplitOptions.RemoveEmptyEntries)
myTool.Label9.Text = MdlNameArr(1).ToString
I expect the result will be "hotchkis_type1a" but it returns "fa".
It doesnt recognize single underscore "_".
Is there any method to use it properly?
You need to split on a string rather than just a character, so if we look at the available overloads for String.Split, we find the nearest one to that is String.Split(string(), options) which takes an array of strings as the separators and requires the inclusion of StringSplitOptions like this:
Dim s = "sub_fa__hotchkis_type1a__180310__PUO4x4__180813"
Dim separators() As String = {"__"}
Dim parts = s.Split(separators, StringSplitOptions.None)
If parts.Length >= 2 Then
Console.WriteLine(parts(1))
Else
Console.WriteLine("Not enough parts found.")
End If
Outputs:
hotchkis_type1a

Vb.net, replace any number at the end of a string with a placeholder

I have many strings that have numbers at the end. The numbers can be of any size, for example:
myvar123
mysecondvar3
mythirdvar219107
The strings can have numbers even inside the name, not only at the end.
for example:
my2varable123
some123variable9480395
I would need to replace any number at the END with a placeholder. (NOT those inside the varname.)
for example:
my2varable123 should become: my2variable%placeholder%
some123variable9480395 should become: some123variable%placeholder%
The only way that comes to my mind is to go through the string using .right() and remove the char if it is numeric until I find the first non numeric char. Then in the end append the placeholder, but it looks like a lot of work for a rather simply problem.
Is there a better way to do this?
You could use a Regex for this.
Dim str As String = "some123variable9480395"
Dim pattern As String = "\d+$"
Dim replacement As String = "%placeholder%"
Dim rgx As Regex = New Regex(pattern)
Dim result As String = rgx.Replace(str, replacement)

Replace Double Double Quotes in UTF-8 String to single Double quotes

I have string that is formatted in UTF-8.
"{""messages"":[{""messageId"":""245043"",""campaignId"":""14085""
I need to replace the double double quotes to single double quotes.
The following is able to replace double quotes to double single quotes
NewMessage = Replace(Message, "““", "''")
But I can't figure out how to replace the double double quotes to single double quotes.
The code was written in vb.net and the desired output is:
"{"messages":[{"messageId":"245043","campaignId":"14085"
Added image from watch
Solution Provided by o_O
You can leverage the Chr function for this:
NewMessage = Message.Replace(Chr(34) & Chr(34), Chr(34))
I assume your data is coming from somewhere in
"{""messages"":[{""messageId"":""245043"",""campaignId"":""14085"" format, so for the sake of the question I'm formatting the input in a proper way:
Dim str As String = """{""""messages"""":[{""""messageId"""":""""245043"""",""""campaignId"""":""""14085"""""
Console.WriteLine("Input : " & str)
str = str.Replace("""","'").Replace("''","""").Replace("'","""")
Console.WriteLine("Output : " & str)
Output:
Input : "{""messages"":[{""messageId"":""245043"",""campaignId"":""14085""
Output : "{"messages":[{"messageId":"245043","campaignId":"14085"
Check it on Fiddle

Converting Double to String with desired decimal separator

I have to convert number (double) to string like this:
Dim myDouble = 3.14
Dim myDoubleStr = myDouble.ToString ''OR myDouble.ToString("N")
According to my 'culture' settings result is "3,14" what is in most cases OK.
But here are cases that I need string representation of a number with decimal point instead of comma.
In that case I replace char "," with "." like string manipulation.
Is here a way that "ToString" convert a number with decimal point directly when this is needed?
Try
.ToString("F", CultureInfo.InvariantCulture)
More info here
http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#DFormatString
You can also have as much precession as you want by specifying the format like this:
Dim myDouble As Double = 3.14159268
Dim myDoubleStr = myDouble.ToString("0.00000") 'The value will be 3.14159
In case you wanted to use Thousands separator, use this format:
Dim myDouble = 961327.1234567890
Dim MyDoubleStr = myDouble.ToString("#,##0.00000")
'The value of MyDoubleStr will be 961,327.12345

replace " in vb.net

How can I replace the double quote in VB.NET?
This code doesn't work:
name.Replace("""," ")
You need to use a double quote within those quotes (and get the return value - String.Replace does not operate on the string itself, it returns a new string):
name = name.Replace(""""," ")
Instead of a "data link escaped" method of...
name = name.Replace("""", "")
You could be explicit and somewhat more readable...
name = name.Replace(ControlChars.DblQuote, "")
And BTW, instead of thinking of this as returning a NEW STRING; it's better to think of the REPLACE as a part of the STRING Class associated with the 'name' instance. If it's losing the old value of name that you do not want then simply...
Dim aNewString$ = name.Replace(ControlChars.DblQuote, "")
And the 'name' will remain unchanged.
name = name.Replace(Chr(34), "")
you should return the resultant string back to a string and also escape that double quotes with a double quote or "\"
name = name.Remove("""", String.Empty)
I had a nasty one where try as I might, I could not get Replace() to work. In the end, it turned out the strings I was trying to clean somehow had got a completely different characters which just LOOKED like double quotes. A genius had edited a script file using Word, so "hello" became “hello”. Subtle, or what?
Looking at the file with a hex editor, the opening quote was the three character value 0xe2 0x80 0x9c, and the closer was 0xe2 0x80 0x9d. No wonder the replace failed!
'This part is to remove the " mark in the string
Dim GetDate31 As String = Date31(16).Replace(Chr(34), "")