Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Putting Quotation Marks in a String Programmatically

Putting Quotation Marks in a String Programmatically (Windows Forms)

Visual Studio .NET 2003

Sometimes quotation marks (" ") appear in a string of text. For example:

   She said, "You deserve a treat!"

To place quotation marks in a string in your code

  • In Visual Basic, insert two quotation marks in a row as an embedded quotation mark. In Visual C# and Visual C++, insert the escape sequence \" as an embedded quotation mark. For example, to create the preceding string, use the following code:
    ' Visual Basic
    Private Sub InsertQuote()
       TextBox1.Text = "She said, ""You deserve a treat!"" "
    End Sub
    
    // C#
    private void InsertQuote(){
       textBox1.Text = "She said, \"You deserve a treat!\" ";
    }
    
    // C++
    private:
       void InsertQuote()
       {
          textBox1->Text = S"She said, \"You deserve a treat!\" ";
       }
    

    -or-

  • Insert the ASCII or Unicode character for a quotation mark. In Visual Basic, use the ASCII character (34). In Visual C#, use the Unicode character (\u0022):
    ' Visual Basic
    Private Sub InsertAscii()
       TextBox1.Text = "She said, " & Chr(34) & "You deserve a treat!" & Chr(34)
    End Sub
    
    // C#
    private void InsertAscii(){
       textBox1.Text = "She said, " + '\u0022' + "You deserve a treat!" + '\u0022';
    }
    
    Visual C++ Note   In this example, you cannot use \u0022 because, you cannot use a universal character name that designates a character in the basic character set. Otherwise, you produce C3851. For more information, see Compiler Error C3851.

    -or-

  • You can also define a constant for the character, and use it where needed:
    ' Visual Basic
    Const quote As String = """"
    TextBox1.Text = "She said, " & quote & "You deserve a treat!" & quote
    
    // C#
    const string quote = "\"";
    textBox1.Text = "She said, " + quote +  "You deserve a treat!"+ quote ;
    
    // C++
    const String* quote = S"\"";
    textBox1->Text = String::Concat(S"She said, ",
       const_cast<String*>(quote), S"You deserve a treat!",
       const_cast<String*>(quote));
    

See Also

Introduction to the Windows Forms TextBox Control | Controlling the Insertion Point in a Windows Forms TextBox Control | Creating a Password Text Box with the Windows Forms TextBox Control | Creating a Read-Only Text Box (Windows Forms) | Selecting Text Programmatically in the Windows Forms TextBox Control | Viewing Multiple Lines in the Windows Forms TextBox Control | TextBox Class | TextBox Control (Windows Forms)

Show:
© 2015 Microsoft