简体   繁体   中英

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\\" ?

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 appended 1 .

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...

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.

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, eg

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

Which will achieve the same thing.

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.

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+="\";
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.

Try this

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

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