My code here:
String text = "" + label1.Text + ""; //label1 is: C:\myfiles\download
textBox1.Text = text;
I would textbox shown after I built: (has quotes)
"C:\myfiles\download"
Please help me.
Thank you very much. Sorry my English is bad.
My code here:
String text = "" + label1.Text + ""; //label1 is: C:\myfiles\download
textBox1.Text = text;
I would textbox shown after I built: (has quotes)
"C:\myfiles\download"
Please help me.
Thank you very much. Sorry my English is bad.
In order to avoid such errors, use formatting:
textBox1.Text = String.Format("\"{0}\"", label1.Text);
and let compiler ensure that the provided string "\"{0}\""
is free from typos.
This?
String text = "\"" + label1.Text + "\""; //label1 is: C:\myfiles\download
This escapes the quotes meaning: the character after the \
has no special meaning any more and is a usual character.
Or even easier using verbatim-string:
String text = @"""" + label1.Text + @""""; //label1 is: C:\myfiles\download
You need to use backslashes. The second occurence of " will escape the string declaration. Backslashes will prevent this.
String text = "\"" + label1.Text + "\"";
textBox1.Text = text;
You don't need to declare a variable, except you use this value again. Futhermore, you can use string.Format()
. For more information about this method, watch the link in the references section.
textBox1.Text = string.Format("\"{0}\"", label1.Text);
This Stackoverflow-question focuses on the same problem: How to use a string with quotation marks inside it?
Here a MSDN reference: https://msdn.microsoft.com/en-us/library/aa983682(v=vs.71).aspx
Using C# 6, you can very neatly do this using the following syntax:
textBox1.Text = $"\"{label1.Text}\"";
This is shorthand for textBox1.Text = String.Format("\"{0}\"", label1.Text);
and, as with String.Format
, the compiler will check the validity of the string for you.
Double quotation mark represented by \"
as an escape sequence character like;
String text = "\"" + label1.Text + \""";
Or you can double it with a verbatim string literal as;
String text = @"""" + label1.Text + @"""";
Or you can use string.Format
as;
String text = string.Format("\"{0}\"", label1.Text);