在代码中的字符串内放置引号

  • 在 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));  
    
  • TextBox
  • Quote
  • TextBox 控件概述
  • 如何:控制 Windows 窗体 TextBox 控件中的插入点
  • 如何:使用 Windows 窗体 TextBox 控件创建密码文本框
  • 如何:创建只读文本框
  • 如何:在 Windows 窗体 TextBox 控件中选择文本
  • 如何:在 Windows 窗体 TextBox 控件中查看多个行
  • TextBox 控件
  •