views:

202

answers:

10

Hello,

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

+1  A: 
string profileArg = "/profile \"eScore\" ";
Jeroen
A: 

Try this:

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

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

drpcken
nope. don't use the at sign then.
jgauffin
Sorry I didn't mean to leave the @ sign.
drpcken
This does not compile
Carlos Muñoz
+11  A: 

You appear to already be doing that correctly.

Capital G
+1  A: 
string profileArg = "/profile \"eScore\" ";
lumberjack4
The backslash isn't used for escaping in a verbatim string (@ makes it verbatim) use a double "" instead (like VB.)
Adam Ruth
Just caught that mistake
lumberjack4
A: 

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.

Robaticus
+1  A: 

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".
McKay
+11  A: 

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\"";
jgauffin
+1  A: 

One possibility would be

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

To me this looks clearer than the verbatim literal

Martijn
A: 

Use quotation mark as a character like so...

String profileArg = "/profile " + '"' + "eScore" + '"';
mattyB
A: 

here you go

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