有时,你可能想要将引号(“”)放在文本字符串中。 例如:
她说,“你值得奖励一下!”
或者,还可以将 Quote 字段用作常量。
在代码中的字符串内放置引号
在 Visual Basic 中,将两个引号插入一行中作为嵌入引号。 在 Visual C# 和 Visual C++ 中,插入转义序列 \" 作为嵌入引号。 例如,若要创建前面的字符串,请使用以下代码。
Private Sub InsertQuote() TextBox1.Text = "She said, ""You deserve a treat!"" " End Sub
private void InsertQuote(){ textBox1.Text = "She said, \"You deserve a treat!\" "; }
private: void InsertQuote() { textBox1->Text = "She said, \"You deserve a treat!\" "; }
-或-
为引号插入 ASCII 或 Unicode 字符。 在 Visual Basic 中,使用 ASCII 字符(34)。 在 Visual C# 中,使用 Unicode 字符(\u0022)。
Private Sub InsertAscii() TextBox1.Text = "She said, " & Chr(34) & "You deserve a treat!" & Chr(34) End Sub
private void InsertAscii(){ textBox1.Text = "She said, " + '\u0022' + "You deserve a treat!" + '\u0022'; }
注释
在此示例中,无法使用 \u0022,因为不能使用在基本字符集中指定字符的通用字符名称。 否则,将生成 C3851。 有关详细信息,请参阅 编译器错误 C3851。
-或-
还可以定义字符的常量,并根据需要使用它。
Const quote As String = """" TextBox1.Text = "She said, " & quote & "You deserve a treat!" & quote
const string quote = "\""; textBox1.Text = "She said, " + quote + "You deserve a treat!"+ quote ;
const String^ quote = "\""; textBox1->Text = String::Concat("She said, ", const_cast<String^>(quote), "You deserve a treat!", const_cast<String^>(quote));