Harvey
Harvey

Reputation: 1408

VB.NET, adding a double quote character to string

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?

Upvotes: 8

Views: 38845

Answers (4)

Leo Garsia
Leo Garsia

Reputation: 303

msgbox(string.format("hello {0}world{0}", chr(34)))

Upvotes: 0

Ahmed Sabri
Ahmed Sabri

Reputation: 27

Well it's Very eazy just use this : ControlChars.Quote "Hello, " & ControlChars.Quote & "World" & ControlChars.Quote

Upvotes: 0

Steven Doggart
Steven Doggart

Reputation: 43743

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\"".

Upvotes: 15

Martin Soles
Martin Soles

Reputation: 559

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.

Upvotes: 2

Related Questions