Add quote

  • Thread starter Thread starter Alan T
  • Start date Start date
A

Alan T

I want to add a double quote to a string:
"John"

Why it should work this way:
String name = "John";
String myName = @"""" + name + @"""";

But not
String myName = @""" + name + @""";

As I added a @, I think just one double quote is enough.
 
Actually, the first way you show wouldn't need the verbatim operator since
you are escaping the quotes anyway.

I think the problem with the second way you show is that you are using the
verbatim operator twice in one string. I believe it's meant more for
literal strings like:

myName = @""Scott""
 
Alan T said:
I want to add a double quote to a string:
"John"

Why it should work this way:
String name = "John";
String myName = @"""" + name + @"""";

But not
String myName = @""" + name + @""";

As I added a @, I think just one double quote is enough.

No. Within a verbatim string literal, double quotes are represented as
"", so @"x""y""z" is x"y"z.

I'd suggest using a non-verbatim string literal:

string name = "John";
string myName = "\"" + name + "\"";
 
Alan T said:
I want to add a double quote to a string:
"John"

Why it should work this way:
String name = "John";
String myName = @"""" + name + @"""";

But not
String myName = @""" + name + @""";

As I added a @, I think just one double quote is enough.

There -is- one special character sequence in @ed strings: "" represents ".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top