0

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.

Cool
  • 25
  • 5

5 Answers5

2

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.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

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
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
1

Solving your problem

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;

Now, a little refactoring

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);

Some references

Community
  • 1
  • 1
Jules
  • 567
  • 1
  • 6
  • 15
1

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.

David Arno
  • 42,717
  • 16
  • 86
  • 131
0

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);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364