Home » SQL Server

Backslash issue

I'm creating a console app where I pass in a full file path to a specific file that I use for my app.  When I pass in the path it has a single backslash for each directory
but when I look at args[0] it displays it like this

My problem is that I use the length function of the string variable that I set equal to args[0].tostring().  The length that appears is the length of the string with only single backslashes.  I'm using a substring to find the word "Data" and get the position of it.  I'm trying to end up with C:\temp\2009\testing\data\.  My problem is that length is with the single slash but when i'm working with the path it's the double slash so my calculation isn't working out.

Thank you


10 Answers Found


Answer 1

It's just an escaped string.  The .Length property should recognize this and only return the proper length  of a string.  In other words, "\\" is "\", it's just represented as two slashes, because "\" is the escape character in C#.  To indicate that you really want a backslash, and don't want to escape the next character, you have to escape the backslash.

The Length property is unaffected by this, and counts "\\" as one character, the backslash.

See this link:


Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light WikiTwitterLinkedInForumsBrowser

Answer 2

To avoid confusion use the literal @ before the string  so the compiler knows that the string is to be taken as is without worry about character escapes. So these two strings are the same at runtime:

string abc = "C:\\test\\subdirectory\\filename.txt";

string def = @"C:\test\subdirectory\filename.txt";


Answer 3

Thank you to both of you for your replies they made sense of my issue.

My only problem  is how to implement this when I have a variable  that is holding my string.  The string  is an argument that is passed into my console  app so I'm not sure how to put @ on it.

Thank You

Answer 4

You won't be able to.  The syntax that William gave is only applicable if you're putting the string  within a C# application.  You'll have to simply "know" that the double slash  represents a single  slash.  The debugger is always going to show it that way, regardless of whether you use the @ sign or not when hard-coding the string in the C# application. 

Just read the page I linked to, and know that the debugger always represents the string not as a verbatim string literal, but as a regular string literal.

Answer 5

Thank you to both of you for your replies they made sense of my issue.

My only problem  is how to implement this when I have a variable  that is holding my string.  The string  is an argument that is passed into my console  app so I'm not sure how to put @ on it.

Thank You

The user will not be escaping the \ for compile reasons. If one is placing it in the code one has to escape for the compiler. The user will simply say


which is handled as a literal just as if one said in code

string abc = @"C:\abc";


string abc = "C:\\abc";


Answer 6

Not sure this is clear.  The debugger is not showing you the actual content of the string.  It is changing it to the format you'd have to use to write it as a string  literal in your program.  To see the real string, put the variable  in the Watch window and click the magnifier icon.

Answer 7

in your examples both strings will have the same length  even though dont.

Length = 6
string abc = @"C:\abc";


Length = 6 (It should be 7)
string abc = "C:\\abc";

My problem  is that my variable  contains the string  passed in from the command line argument but with the double  slashes. The length property is based on a single  slash.

Thank You for your patience



Answer 8

No, you're still not getting it. Let me try to explain with more detail, and a little more step by step:

Let's say that I have to write a computer program that reads a string  character by character.  I've decided that I need to use double  quotes to signify the beginning and the end  of a string.  Knowing this, I can represent the string


in my code file  by typing:


Great, no problem.  But, what if I want to signify the following:

Bob says, "Hello."

Now I have a problem.  The text I want to represent contains a quote character.  So doing this won't work:

"Bob says, "Hello""

Because, the quote signifies the beginning and the ending of ths string. So what do I do?  I use a special character that means "the next character should be considered part of the string, and not signify the beginning or the end of the string.  I'll make that character be a "\". So now, I can type the following to represent my desired string:

"Bob says, \"Hello\""

Preceding the quote symbol with a backslash  means that the next character is going to be a quote that is considered part of the string, not the end of the string.  Not preceding it means that I'm signifying the end or beginning of the string. 

Now that I've done that, I have another problem.  What if I want to represent this character.  For example, what if I have a string like the following:

C:\Program Files\Morton\

Now since I've redefined my backslash to mean that the next character is to be taken literally and not as a bounds to the value I'm trying to represent, putting the "\" in at all is now a very difficult thing.  

So what do I do?  I follow that backslash with another backslash to represent the end of the string.  So now, to represent the string, I do this:

"C:\\Program Files\\Morton\\"

The above format is what the C# debugger will show when you hover over a particular string variable  containing the text:

C:\Program Files\Morton\

In other words, it shows the value as it would need to be written in the source file, and not as it actually is.  

So what about when you need to represent the following text:

When two backslashes (\\) are together, it means we want to represent a single  "\" character. 

How would you format that in the source code?  Like this:

"When two backslashes (\\\\) are together, it means we want to represent a single \"\\\" character."

Confusing, I know, but the idea is still the same.  My actual text I want to represent contains two backslashes in succession, so to represent that, I'd have to put four backslashes in succession.  Also, note how the characters following "single" and before "character" are represented, by using a backslash, quote, backslash, backslash, backslash, quote respectively. 

This is what it means to escape a string.  Though string appear escaped in the debugger, this does not add to their length.   The runtime still recognizes an escaped character as simply one character and not two.  In other words, it does not take into account the escape character (\) when calculating the length  of the escaped character (\\), 


Answer 9

Thank you so much.  After I read your post I got it but not all the way.  I cleared my head and re-read it and looked at my code and it makes total sense and I got it working. 

Thank you and thanks to everyone else who posted.


Answer 10

I know this is an old thread, but I thought I add this for your information.

I needed to change a string  containing double  backslashes into the same string with single  backslashes and simply used:

String ret = thisStr.Replace("\\\\","\\");


Search for  Backslash issue


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure