Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET, adding a double quote character to string

Tags:

vb.net

Everytime I add CharW(34) to a string it adds two "" symbols

Example:

text = "Hello," + Char(34) + "World" + Char(34)

Result of text

"Hello,""World"""

How can I just add one " symbol?

e.g Ideal result would be:

"Hello,"World""

I have also tried:

text = "Hello,""World"""

But I still get the double " Symbols

Furthermore. Adding a CharW(39), which is a ' symbol only produces one? e.g

text = "Hello," + Char(39) + "World" + Char(39)

Result

"Hello,'World'"

Why is this only behaving abnormally for double quotes? and how can I add just ONE rather than two?

like image 680
Harvey Avatar asked Oct 01 '15 15:10

Harvey


2 Answers

Assuming you meant the old Chr function rather than Char (which is a type).It does not add two quotation mark characters. It only adds one. If you output the string to the screen or a file, you would see that it only adds one. The Visual Studio debugger, however, displays the VB-string-literal representation of the value rather than the raw string value itself. Since the way to escape a double-quote character in a string is to put two in a row, that's the way it displays it. For instance, your code:

text = "Hello," + Chr(34) + "World" + Chr(34)

Can also be written more simply as:

text = "Hello,""World"""

So, the debugger is just displaying it in that VB syntax, just as in C#, the debugger would display the value as "Hello, \"World\"".

like image 83
Steven Doggart Avatar answered Oct 23 '22 14:10

Steven Doggart


The text doesn't really have double quotes in it. The debugger is quoting the text so that it appears as it would in your source code. If you were to do this same thing in C#, embedded new lines are displayed using it's source code formatting.

Instead of using the debugger's output, you can add a statement in your source to display the value in the debug window.

Diagnostics.Debug.WriteLine(text)

This should only show the single set of quotes.

like image 24
Martin Soles Avatar answered Oct 23 '22 15:10

Martin Soles