How to quote \ "(slash) in a string literal?

This is probably a very simple question, but I seem to be unable to think about it. I need to have a string containing \" without seeing it as an escape character. I tried using @ , but this will not work. The only other way I thought to do this was to use \u0022 , but I don’t want it if I don’t I can help him.

The desired string is string s = "\"\""; // Obviously this doesn't work! string s = "\"\""; // Obviously this doesn't work!

The desired console output is \"\"

Hope this makes sense.

Thanks!

+7
source share
9 answers

Try

 string s = "\\\"\\\""; 

You also need to avoid backslashes.

Mike

+6
source

You can use a literal, but you need to double the quotation marks.

  string s = @"\""\"""; 
+5
source

I think you need to hide the backslash too ... so it seems to me something like "\\\"\\\"" .

+4
source

In string literals, strings ( @"..." ) a " in string value is encoded as "" , which is also the only escape sequence in shorthand strings.

 @"\""Happy coding!\""" // => \"Happy coding!\" "\\\"Happy coding!\\\"" // => \"Happy coding!\" 

Note that in the second case (and not in the form of a shorthand literal) before \ and " is required \ in order to avoid their normal values.

See the link to the C # line for more details.

+4
source

Use this line:

 string s = "\\\"\\\""; 
+1
source
 Console.WriteLine( "\\\"\\\"" ); 

Just put \ in front of each character you want to print.

+1
source
 String s = @"\""\"""; 

DblQuote characters escape the second dblquote character

Although for better readability I would go with:

 const String DOUBLEQUOTE = """"; const String BACKSLASH = @"\"; String s = BACKSLASH + DOUBLEQUITE + BACKSLASH + DOUBLEQUOTE; 
+1
source

In a shorthand line (a line starting with @"" ), to avoid double quotes, you use double quotes, for example. @"Please press ""Ok""." . If you want to do this with shorthand lines, you will do something like @"\""" (which has three double quotes on this page).

+1
source

You can do it,

 string s = "something'\\\'"; 

Use the single character '', not "" in the string, to do the same.

0
source

All Articles