Friday, January 04, 2008

Inserting a Double Quote in to a String in .NET

Even though this is not new I do forget this always, so thought of putting an entry on how we can put a double quote inside of a string.

VB.NET
Dim str As String = "Example String " & """" & "This is the String with double quotes." & """"
MessageBox.Show(str)

In VB.Net you can indicate that there is a double quote in a string by using 4 double quotes ("""").

C#.NET
string str = "Example String " + "\"" + "This is the String with double quotes." + "\"";
MessageBox.Show(str);

As you will notice in C# the double quote can be represented as double quote, back slash and again using two double quotes ("\"").

The above string will be displayed as follows.

image

11 comments:

Neelmani Pathak said...

thanx

Anonymous said...

thanx, that solve my problem.

Anonymous said...

Thanx Man U r G8...

shantanu said...

Thanks Gud one. Solved my prob.

Anonymous said...

An impressive share! I have just forwarded this onto a co-worker who was doing a little research on this.
And he actually ordered me lunch due to the fact that I discovered it for
him... lol. So allow me to reword this.... Thanks for
the meal!! But yeah, thanx for spending the time to talk about this subject here on your web site.
My web site :: Poesie

Bhasker Reddy said...

thankyouuuuu so much

Bhasker Reddy said...

Thnx

Anonymous said...

I need help with a VB program I am working on this is what I have and it doesn't work. I need to combine the text box text, and the comment I wrote in.

Private Sub txtcomment_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcomment.TextChanged
comment = "" & txtcomment.Text + "You have: " + time + " seconds until your computer shuts down. :)" & ""
End Sub

Help thanks!

Arjuna said...

Use multiple double quotes to make them appear in your output.
Try to get an idea using the below code.
Let me know if you still have problems.

Dim time As String = "60"
' If you need to use a quoteation in a variable use 4 quotes as in the begining of the following code.
Dim comment As String = """" & txtcomment.Text + "You have: " + time + " seconds until your computer shuts down. :)"""
MsgBox(comment)
MsgBox("""" & txtcomment.Text + "You have: " + time + " seconds until your computer shuts down. :)""")
MsgBox("""Text with double quotes.""")

Anonymous said...

Many thanks Arjuna! This is one of those nuggets that leaves you thinking 'Why don't I know this?!'... but only when you need it!

Anonymous said...

Hello! I really liked your forum, especially this section. I just signed up and immediately decided to introduce myself, if I'm wrong section, ask the moderators to move the topic to the right place, hopefully it will take me well... My name is Igor, me 34 years, humourist and serious man in one person. Ssory for my English.