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"
In
VB .Net
you can do this :Assuming
count = 1 (Integer), pt1 = "NAME" and pt2 = "HERE"
.The output will be Dim Choice As String = "Your New Name is: NAME_HERE"
As Tim said, simply replace each occurrence of
"
inside the string with""
.Furthermore, use
String.Format
to make the code more readable: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:You have to escape the quotes. In VB.NET, you use double quotes - "":
This would print as:
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:
Which would print as:
Although escaping the string is the correct way of doing things it is not always the easiest to read. Consider trying to create the following string:
To escape this you need to do the following:
But if you using
String.Format
withChr(34)
you can do the following:This is an option if you find this easier to read.
You have to escape them, however you cannot dynamically generate variable names as you're attempting to here:
this will just be a string.
You can use the Chr Function with the quotes ASCII Code: 34 to have a result as this: