How to insert single quotes in double quotes in VB

2019-07-17 06:50发布

问题:

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>")

回答1:

1st:

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

  • Left Double Quotation Mark
  • Right Double Quotation Mark

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)


回答2:

It seems you used the wrong character for single quotation:

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


回答3:

not sure if this works or not:

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

or maybe even this

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


回答4:

You will have to change the &"""" with chr(24)

so you would have something like this objWriter.WriteLine("

  • " & chr(34 ) & chr(34) & "" + BP1.Text + ". . . . .