Replace Double Double Quotes in UTF-8 String to single Double quotes - vb.net

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

Related

Remove single quote (") from string and replace double quotes ("") with single quotes (") in VB.NET

I have a string similar to this one in VB.NET:
Dim str as String = """This is an """"example"""" string"""
Which looks like this in plaintext: "This is an ""example"" string"
What I want to do is remove the single quotes from this value and replace the double quotes with single quotes: This is an "example" string
I don't see a way of doing this that wouldn't also remove the double quotes.

remove from String in VB

I have inserted a option in Dorpdown as follows
<option>إختر </option>
When I select this text from server side on any event I get this value
"إختر       ‎"
Now I want to replace this white space in the string. I have tried replace method of String class. But its not working.
str = str.replace(" ","")
Plz suggest
What you should do first is decode the HTML, such that text like but also & are converted to their textual counterparts (" " and "&"). You can do this with: WebUtility.HtmlDecode. Next you can use String.Trim to remove leading and tailing spaces.
Example:
string s = "إختر ";
string r = WebUtility.HtmlDecode(s).Trim();
Or the VB.NET equivalent:
Dim s As String = "إختر "
Dim r As String = WebUtility.HtmlDecode(s).Trim()
Evidently you can try to convert to spaces yourself. But there are examples where it is not that evident and your transcoder can get confused or decode strings the wrong way. Furthermore if in the future the people at W3C change their minds about how to encode text in HTML/XML, then your program will still work.
String.Trim will remove all kinds of white-space including spaces, new lines, tabs, carriage returns, etc. If you only want to remove spaces, you can use: .Trim(' '). Then you specify only to remove the given list of characters (here only ' ').
If you want to remove leading or trailing white-spaces from a string you just need to use String.Trim, but you have to re-assign the return value to the variable since strings are immutable:
string text = "إختر       ‎";
text = text.Trim();
Note that you can also use TrimEnd in this case.
If you want to remove only space characters(not also tabs or new-line characters which are also white-spaces) use:
text = text.Trim(' ');
If you instead want to remove all spaces from a string you could do:
text = text.Replace(" ", "");
I think maybe your code is something like this
Dim str As String = "إختر "
str.Replace(" ", "")
But actually you should
Dim str As String = "إختر "
str = str.Replace(" ", "")
I have just had a similar problem.
It turns out, that this nbsp character is Chr(160) from the ASCII table. Thus, something like this is quite meaningful, for all the cases. It works, on a selected area:
Public Sub remove_space_in_string()
Dim r_range As Range
For Each r_range In Selection
r_range = Trim(r_range)
r_range = Replace(r_range, vbTab, "")
r_range = Replace(r_range, " ", "")
r_range = Replace(r_range, Chr(160), "")
Next r_range
End Sub

Removing the double quotes from a string

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.

How to make a variable that holds double qoutes

The question is simple, How do I define a variable which holds double quotes. when I try to define the variable like this
Dim s as String = " " " ,
VS puts an extra quote like this
Dim s as String = """"
The extra " is used to escape the " character, so the sequence of two double-quotes ("") will show up as " when your string is displayed.
You actually have it correct with the 4 quotes, that is VBs way of escaping the quotes. So, for example:
Dim oneDoubleQuote As String = """"
Dim twoDoubleQuotes As String = """"""
MessageBox.Show("One:" & oneDoubleQuote)
MessageBox.Show("Two:" & twoDoubleQuotes)
The first message box has One:" and the second has Two:""

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), "")