Include quotes inside a string?

I am trying to include quotes in my string to add to the text box, I am using this code.

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

but it does not work, I want it to be output as follows:

 Dim Choice As String = "Your New Name is: NAME_HERE" 
+6
source share
6 answers

You need to avoid quotes. In VB.NET, you use double quotes - "":

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

This will print as:

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

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

If count not an integer, you can remove the ToString () call.

In C # you avoid "using a \, for example:

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

What will print as:

 string Choice1 = "Your new Name is: NAME HERE"; 
+13
source

As Tim said, just replace each occurrence " inside the string "" . ""

Also, 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 may even be a method that supports format strings directly, i.e. you might even simplify the above:

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

You need to avoid them, however you cannot dynamically generate variable names as you are trying here:

 "Dim Choice" & count + " As String = " 

it will be just a string.

0
source

You can use Chr Function with ASCII Code : 34 quotes to get the result:

 t.Append(Dim Choice As String = " & Chr(34) & "Your New Name is: NAME_HERE" & Chr(34)) 
0
source

Although string escaping is the right way to do things, it is not always the easiest to read. Try creating the following line:

 Blank "" Full "Full" and another Blank "" 

To avoid this, you need to do the following:

 "Blank """" Full ""Full"" and another Blank """"" 

But if you use String.Format with Chr(34) , you can do the following:

 String.Format("Blank {0}{0} Full {0}Full{0} and another Blank {0}{0}", Chr(34)) 

This is an option if you find it easier to read.

0
source

In VB .Net you can do this:

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

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

The output will be Dim Choice As String = "Your new name: NAME_HERE"

0
source

Source: https://habr.com/ru/post/896182/


All Articles