Click here to Skip to main content
15,908,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to change this

H:\app\new\data\


Like this

H:\\app\\new\\data\\

How to do this
I use this but there is an error
C#
string old = "H:\app\new\data\";
string new = Regex.Replace(old,"\","\\"); //Error

i'm new to C#
Thank in advance!
Posted

That's down to C# and how it handles strings with backslashes in them. Try this:
C#
string old = @"H:\app\new\data\";
Or:
C#
string old = "H:\\app\\new\\data\\";
To remove compiler errors, then (if you really need to do this, and I doubt if you do:
C#
string new = Regex.Replace(old,@"\",@"\\");


Backslach is a special character in C# string: it acts as an "escape" character to allow you to enter spacial values such as
double quote    \"
backslash       \\
newline         \n
and so forth.
When VS shows strings containing a genuine single backslash, it shows it as two to indicate that it is escaped and not a part of the following character.
 
Share this answer
 
Comments
Manoj Chamikara 20-Oct-13 10:17am    
Thanks for your response
No errors but an error occurred application runtime

parsing "\" - Illegal \ at end of pattern.
OriginalGriff 20-Oct-13 10:35am    
Yes - because backslash is a special character in Regexes as well - so if you want to actually replace a single backslash in a string with two backslashes, using a regex, you will actually need to do this:
string newString = Regex.Replace(oldString, @"\\", @"\\");
Or
string newString = Regex.Replace(oldString, "\\\\", "\\\\");
Both of which look very, very wrong! :laugh:
And when you look in VS, it will look even wrong-er...
But as I said, I strongly suspect you don't need to do this at all!


You may need to understand escaping in C# first. Have a look at my article about Escaping in C#: characters, strings, string formats, keywords, identifiers[^].

For your example, you don't need regex, you can use string.replace(...)[^], e.g.,
C#
string oldString = @"H:\app\new\data\";
string newString = oldString.Replace(@"\",@"\\");

Please also note that new is a keyword that you cannot use as variable name as you do in your question.
Cheers
Andi
 
Share this answer
 
v2
Comments
Manoj Chamikara 20-Oct-13 10:19am    
It's Works Thanks for your help :)
Andreas Gieriet 20-Oct-13 10:23am    
You are welcome.
Cheers
Andi
OriginalGriff has provided you with a great answer. Note that .NET also includes the string.Replace function, which can replace either a single character (Type char) with another single character, or a string with a string.

string s = @"\1\2\3\4\5\6";

s = s.Replace(@"\", @"\\");

If you set a break-point and inspect the variable 's at run-time after the Replace operation you'll see:

"\\\\1\\\\2\\\\3\\\\4\\\\5\\\\6"

But, the length of the modified 's will be #18.

You'll note that calling string.Replace(string target, string replace) returns a new string from its operation. All string operators create copies of the string they manipulate because strings in .NET are immutable.
 
Share this answer
 
Comments
Manoj Chamikara 20-Oct-13 10:25am    
Thank you Bill for clearly details with examples :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900