简体   繁体   中英

How do I create a string with Quotes in it in C#

C# newbie here. I am trying to set a C# variable as follows.

string profileArg = @"/profile ""eScore"" ";

The end result is that I want the variable to contain the value

/profile "eScore" 

There should be a space in the string after the "eScore"

How do I do it?

Seth

你似乎已经正确地这样做了。

You have a space after eScore in your string.

// a space after "eScore"
string profileArg = @"/profile ""eScore"" ";

// no space after "eScore"
string profileArg = @"/profile ""eScore""";

// space in "eScore "
string profileArg = @"/profile ""eScore """;

// No space using escaping
string profileArg = "/profile \"eScore\"";
string profileArg = "/profile \"eScore\" ";
string profileArg = "/profile \"eScore\" ";

2 options:

  • normal backslashed escaped: "This is a test of \\"Quotes\\"."
  • @ string double escaped: @"This is a test of ""Quotes""."

both of these should contain the same string:

This is a test of "Quotes".

One possibility would be

string profileArg = "/profile \"eScore\" ";

To me this looks clearer than the verbatim literal

Try this:

string profileArg = "/profile \"eScore\" ";

You want to put \\" for any literal double quotes.

To add to the others . . . The @ sign that precedes the first quote tells C# to not interpret the backslash \\ as an escape character. That's why the examples given omit the @ sign. Then you can use the \\" to put in the quotation marks.

干得好

String test = "   /profile \"eScore\"     ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM