-2

so basically I'm trying to escape a quotation mark in VB.

I have the following code:

RichTextBox2.Text = "<furnitype id=" + TextBox3.Text + "

And I need:

RichTextBox2.Text = "<furnitype id="" + TextBox3.Text + ""

So basically the text that is within the textbox3 needs to be quoted, but I can't do so since it requires quotation to add text.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Liam
  • 41
  • 1
  • 7
  • Posible duplicate : http://stackoverflow.com/questions/4835691/escape-double-quote-in-vb-string – DiegoS Dec 24 '16 at 03:38
  • @DiegoS I'm not using console. – Liam Dec 24 '16 at 03:40
  • It doesn't matter whether you're using the Console or not. A string is a string. – jmcilhinney Dec 24 '16 at 03:42
  • It doesn't work when I use double quotation. – Liam Dec 24 '16 at 03:43
  • 2
    VB.Net <> basic <> vba <> vbscript <> VB6. Please use only the tags that are actually relevant to your question, instead of just randomly adding those that sound familiar. A *car* is not like a *cat* just because they both start with *ca*. Tags have meaning and relevance here. If you're not sure a tag applies, don't use it; if it's necessary, someone here will add it for you. (And if it *needs quotation but you can't do so since it requires quotation*, just add more quotes on each side. In other words, quote the quotes you need.) – Ken White Dec 24 '16 at 03:50
  • Also, in VB, use & rather than + to concatenate strings ;) – Zeek2 Jul 05 '21 at 16:00

2 Answers2

7

You escape a double quote with another double quote, e.g.

Console.WriteLine("He said ""Hello"" to me.")

will display:

He said "Hello" to me.

In your case, I think you code should be something like this:

RichTextBox2.Text = "<furnitype id=""" & TextBox3.Text & """>"

I would tend to suggest using String.Format or string interpolation to make the code clearer though:

RichTextBox2.Text = String.Format("<furnitype id=""{0}"">", TextBox3.Text)

or:

RichTextBox2.Text = $"<furnitype id=""{TextBox3.Text}"">"

Note that string interpolation is only available in VB 2015 or later.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Then you did it wrong. We can't know what you did wrong if we don't know what you did. – jmcilhinney Dec 24 '16 at 03:41
  • 1
    So, can we assume that it DOES work when you do it right? In future, NEVER just say "it doesn't work" because 9 times out of 10 at a minimum it's the implementation that is wrong, not the principle. If you don't show us what you did then you're wasting our time, whether it's intentional or not. The code that doesn't do what you want is ALWAYS relevant. – jmcilhinney Dec 24 '16 at 04:18
0

Escape Double Quote with an additional double quote. To solve your particular string for you:

RichTextBox2.Text = "<furnitype id=""" + RichTextBox2.Text + """"
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
pizzaslice
  • 478
  • 2
  • 8