Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include quote marks inside a string?

I'm trying to include quotes into my string to add to a text box, i am using this code.

 t.AppendText("Dim Choice" & count + " As String = " + "Your New Name is:  & pt1 + "" & pt2 +" + vbNewLine)

but it doesnt work, i want it to output like so:

Dim Choice As String = "Your New Name is: NAME_HERE"
like image 506
Duncan Palmer Avatar asked Aug 30 '11 07:08

Duncan Palmer


2 Answers

You have to escape the quotes. In VB.NET, you use double quotes - "":

t.AppendText("Dim Choice" + count.ToString() + " As String = ""Your New Name is: "  + pt1 + " " + pt2 + """" + vbNewLine)

This would print as:

Dim Choice1 As String = "Your New Name is: NAME HERE"

Assuming count = 1 (Integer), pt1 = "NAME" and pt2 = "HERE".

If count is not an Integer, you can drop the ToString() call.

In C#, you escape the " by using a \, like this:

t.AppendText("string Choice" + count.ToString() + " = \"Your New Name is: " + pt1 + " " + pt2 + "\"\n");

Which would print as:

string Choice1 = "Your new Name is: NAME HERE";
like image 146
Tim Avatar answered Sep 18 '22 00:09

Tim


As Tim said, simply replace each occurrence of " inside the string with "".

Furthermore, use String.Format to make the code more readable:

t.AppendText( _
    String.Format( _
        "Dim Choice{0} As String = ""Your New Name is: {1} {2}""{3}", _
        count, pt1, pt2, vbNewLine)

Depending on the type of your t, there might even be a method that supports format strings directly, i.e. perhaps you can even simplify the above to the following:

t.AppendText( _
    "Dim Choice{0} As String = ""Your New Name is: {1} {2}""{3}", _
    count, pt1, pt2, vbNewLine)
like image 29
Konrad Rudolph Avatar answered Sep 20 '22 00:09

Konrad Rudolph