Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use \ in a string in C#

Tags:

c#

I want to use \ in a string, like

string str="abc\xyz";

But this is giving me error.

I have also tried

string str="abc\\xyz";

But still it isnt working. Can anyone help me out?

like image 941
Brigadier Jigar Avatar asked Mar 12 '10 12:03

Brigadier Jigar


People also ask

Is %s string in C?

Note: The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to print and read strings directly.

How do you handle double quotes in a string in C #?

To represent a double quotation mark in a string literal, use the escape sequence \". The single quotation mark (') can be represented without an escape sequence. The backslash (\) must be followed with a second backslash (\\) when it appears within a string.

What is a char * in C?

The abbreviation char is used as a reserved keyword in some programming languages, such as C, C++, C#, and Java. It is short for character, which is a data type that holds one character (letter, number, etc.) of data. For example, the value of a char variable could be any one-character value, such as 'A', '4', or '#'.


1 Answers

You can either escape the character, like so:

string str="abc\\xyz";

or use a verbatim string literal like so:

string str=@"abc\xyz";

So your second example should work.

See here for more information.

like image 189
Dave Van den Eynde Avatar answered Oct 08 '22 11:10

Dave Van den Eynde