VB.NET,向字符串添加双引号字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32891836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:10:44  来源:igfitidea点击:

VB.NET, adding a double quote character to string

vb.net

提问by Harvey

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

每次我添加CharW(34)到一个字符串时,它都会添加两个""符号

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

此外。添加一个CharW(39)' 符号只会产生一个?例如

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

Result

结果

"Hello,'World'"

Hello,'World'"

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

为什么这只是双引号的异常行为?我怎样才能只添加一个而不是两个?

回答by Steven Doggart

Assuming you meant the old Chrfunction 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:

假设您指的是旧Chr函数而不是Char(这是一种类型)。它不会添加两个引号字符。它只添加一个。如果您将字符串输出到屏幕或文件,您会看到它只添加了一个。但是,Visual Studio 调试器显示值的 VB 字符串文字表示,而不是原始字符串值本身。由于在字符串中转义双引号字符的方法是将两个字符连成一行,这就是它的显示方式。例如,您的代码:

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

因此,调试器只是以 VB 语法显示它,就像在 C# 中一样,调试器会将值显示为"Hello, \"World\"".

回答by Martin Soles

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.

文本中并没有真正的双引号。调试器正在引用文本,以便它在您的源代码中显示。如果您要在 C# 中做同样的事情,嵌入的新行将使用它的源代码格式显示。

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.

这应该只显示单组引号。

回答by Ahmed Sabri

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

好吧,它非常容易使用: ControlChars.Quote "Hello, " & ControlChars.Quote & "World" & ControlChars.Quote