Search code examples
.netvb.netsyntaxstring-concatenationquotation-marks

How to insert single quotes in double quotes in VB .NET


I'm trying to insert single Quotation marks inside a double quotation marks... Beginning Quotation mark like this "“" ..... and ending Quotation mark like this "”" ...

My code:

objWriter.WriteLine("<li>" + "“" + "<em>" + BP1.Text + "</em>" + "”" + " ― " + "<strong>" + BPGB1.Text + "</strong>" + "</li>")

Solution

  • 1st:

    The characters that you mentioned are not a single quote, are a double quote.

    2nd:

    In Vb.Net, unlike C#, string concatenations are made with the & operator, avoid using the + operator, it will give you unexpected results in some scenarios.


    The Visual Studio's code editor automaticaly replaces the characters that you've mentioned with a common double quote, however, knowing the Unicode references you can get the specific characters at execution time then concat them as normally or using the String.Format() method in this way:

    Dim lQuotes As Char = Convert.ToChar(&H201C) ' “
    Dim rQuotes As Char = Convert.ToChar(&H201D) ' ”
    
    Dim str As String = String.Format("{0}Hello World{1}", lQuotes, rQuotes)
    
    Console.WriteLine(str) ' “Hello World”
    

    UPDATE

    An example with the string that you've provided:

    Dim lQuotes As Char = Convert.ToChar(&H201C) ' “
    Dim rQuotes As Char = Convert.ToChar(&H201D) ' ”
    
    Dim str As String =
        String.Format("<li>{0}<em>{2}</em>{1} ― <strong>{3}</strong></li>",
                      lQuotes, rQuotes, BP1.Text, BPGB1.Text)
    
    objWriter.WriteLine(str)