VB.NET, adding a double quote character to string

前端 未结 3 1800
名媛妹妹
名媛妹妹 2020-12-11 19:22

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

Example:

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


        
相关标签:
3条回答
  • 2020-12-11 19:34

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

    0 讨论(0)
  • 2020-12-11 19:36

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

    0 讨论(0)
  • 2020-12-11 19:50

    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.

    0 讨论(0)
提交回复
热议问题