.net 在字符串中包含引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7239872/
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
Include quote marks inside a string?
提问by Duncan Palmer
I'm trying to include quotes into my string to add to a text box, i am using this code.
我正在尝试将引号包含到我的字符串中以添加到文本框中,我正在使用此代码。
t.AppendText("Dim Choice" & count + " As String = " + "Your New Name is: & pt1 + "" & pt2 +" + vbNewLine)
but it doesnt work, i want it to output like so:
但它不起作用,我希望它像这样输出:
Dim Choice As String = "Your New Name is: NAME_HERE"
回答by Tim
You have to escape the quotes. In VB.NET, you use double quotes - "":
你必须逃避引号。在 VB.NET 中,您使用双引号 - "":
t.AppendText("Dim Choice" + count.ToString() + " As String = ""Your New Name is: " + pt1 + " " + pt2 + """" + vbNewLine)
This would print as:
这将打印为:
Dim Choice1 As String = "Your New Name is: NAME HERE"
Assuming count = 1 (Integer), pt1 = "NAME" and pt2 = "HERE".
假设计数 = 1(整数),pt1 = "NAME" 和 pt2 = "HERE"。
If countis not an Integer, you can drop the ToString() call.
如果count不是整数,则可以删除 ToString() 调用。
In C#, you escape the " by using a \, like this:
在 C# 中,您可以使用 \ 转义 ",如下所示:
t.AppendText("string Choice" + count.ToString() + " = \"Your New Name is: " + pt1 + " " + pt2 + "\"\n");
Which would print as:
将打印为:
string Choice1 = "Your new Name is: NAME HERE";
回答by Konrad Rudolph
As Tim said, simply replace each occurrence of "inside the string with "".
正如蒂姆所说,只需"用"".
Furthermore, use String.Formatto make the code more readable:
此外,用于String.Format使代码更具可读性:
t.AppendText( _
String.Format( _
"Dim Choice{0} As String = ""Your New Name is: {1} {2}""{3}", _
count, pt1, pt2, vbNewLine)
Depending on the type of your t, there might even be a method that supports format strings directly, i.e. perhaps you can even simplify the above to the following:
根据您的类型,t甚至可能有一种直接支持格式字符串的方法,即您甚至可以将上述内容简化为以下内容:
t.AppendText( _
"Dim Choice{0} As String = ""Your New Name is: {1} {2}""{3}", _
count, pt1, pt2, vbNewLine)
回答by Matt Wilko
Although escaping the string is the correctway of doing things it is not always the easiest to read. Consider trying to create the following string:
尽管转义字符串是正确的处理方式,但它并不总是最容易阅读的。考虑尝试创建以下字符串:
Blank "" Full "Full" and another Blank ""
To escape this you need to do the following:
要避免这种情况,您需要执行以下操作:
"Blank """" Full ""Full"" and another Blank """""
But if you using String.Formatwith Chr(34)you can do the following:
但是,如果您使用String.FormatwithChr(34)您可以执行以下操作:
String.Format("Blank {0}{0} Full {0}Full{0} and another Blank {0}{0}", Chr(34))
This is an option if you find this easier to read.
如果您觉得这更容易阅读,这是一个选项。
回答by user3549709
In VB .Netyou can do this :
在VB .Net你可以这样做:
Assuming count = 1 (Integer), pt1 = "NAME" and pt2 = "HERE".
假设count = 1 (Integer), pt1 = "NAME" and pt2 = "HERE".
t.AppendText("Dim Choice" & count.Tostring() + " As String ="+ CHR(34) + "Your New Name is: " & pt1 + "_" & pt2 +CHR(34) + vbNewLine)
The output will be Dim Choice As String = "Your New Name is: NAME_HERE"
输出将是 Dim Choice As String = "Your New Name is: NAME_HERE"
回答by m.edmondson
You have to escape them, however you cannot dynamically generate variable names as you're attempting to here:
您必须对它们进行转义,但是您无法像在此处尝试那样动态生成变量名称:
"Dim Choice" & count + " As String = "
this will just be a string.
这将只是一个字符串。
回答by PedroC88
You can use the Chr Functionwith the quotes ASCII Code: 34 to have a result as this:
您可以使用带引号 ASCII 代码: 34的Chr 函数来获得如下结果:
t.Append(Dim Choice As String = " & Chr(34) & "Your New Name is: NAME_HERE" & Chr(34))

