RRM
RRM

Reputation: 3451

How can I add \ symbol to the end of string in C#

Please forgive me a beginner's question :)

string S="abc";
S+="\";

won't complile.

string S="abc";
S+="\\";

will make S="abc\\"

How can I make S="abc\" ?

Upvotes: 12

Views: 53351

Answers (8)

Utkarsh
Utkarsh

Reputation: 127

Try this

 string s="abc";
 s = s+"\\";

Upvotes: -1

thesheps
thesheps

Reputation: 655

You can escape the backslash with the @ character:

string S="abc";
S += @"\";

But this accomplishes exactly what you've written in your second example. The confusion on this is stemming from the fact that the Visual Studio debugger continues to escape these characters, even though your source string will contain only a single backslash.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500335

Your second piece of code is what you want (or a verbatim string literal @"\" as others have suggested), and it only adds a single backslash - print it to the console and you'll see that.

These two pieces of code:

S += "\\";

and

S += @"\";

are exactly equivalent. In both cases, a single backslash is appended1.

I suspect you're getting confused by the debugger view, which escapes backslashes (and some other characters). You can validate that even with the debugger by looking at S.Length, which you'll see is 4 rather than 5.


1 Note that it doesn't change the data in the existing string, but it sets the value of S to refer to a new string which consists of the original with a backslash on the end. String objects in .NET are immutable - but that's a whole other topic...

Upvotes: 20

Nolonar
Nolonar

Reputation: 6122

string S="abc";
S+="\\";
Console.WriteLine(S); // This is what you're missing ;)

You'll see your string is not wrong at all.

The backslash (\) is an escape character, and allows you to get special characters that you wouldn't normally be able to insert in a string, such as "\r\n", which represents a NewLine character, or "\"" which basically gives you a " character.

In order to get the \ character, you need to input "\\" which is exactly what you're doing and also what you want.

Using the verbatim (@) replaces all occurrences of \ into \\, so @"\" == "\\". This is usually used for paths and regexes, where literal \ are needed in great numbers. Saying @"C:\MyDirectory\MyFile" is more comfortable than "C:\\MyDirectory\\MyFile" after all.

Upvotes: 0

DGibbs
DGibbs

Reputation: 14618

You already have the solution. The reason it appears as abc\\ whilst debugging is because VS will escape backslashes, print the value of S to a console window and you'll see abc\. You could add an @ to the start of the string literal, e.g.

string S="abc";
S+= @"\";

Which will achieve the same thing.

Upvotes: 2

Anri
Anri

Reputation: 6265

Your second example is perfectly fine

string S="abc";
S+="\\";

Visual studio displays string escaped, that's why you see two slashes in result string. If you don't want to use escaping declare string like this

@"\"

This is not compiling because compiler is expecting a character after escape symbol

string S="abc";
S+="\";

Upvotes: 1

hometoast
hometoast

Reputation: 11782

string S = "abs" + "\\";  

Should and does result in abc\.

What you are probably seeing is the way the debugger/intellisense visualizes the string for you. Try printing your string to the console or display it in a textbox.

Upvotes: 4

jerone
jerone

Reputation: 16871

Try this:

String S = "abc";
S += @"\";

@ = verbatim string literal

http://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx

http://msdn.microsoft.com/en-us/library/vstudio/362314fe.aspx

Upvotes: 5

Related Questions