Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
i am trying to replace double backslash into single backslash.but its not happenuig correctly.

Suppose my string is

C#
string str1 = "123\\456\\789\\10";
str1 = str1.Replace(@"\\", @"\");
Response.Write(str1);

it display 123\456\789\10 its correct but on the debug mode value is remain in double backslash format
and i need the value in single backslash.
please reply.
Thannks in advance....
Posted
Updated 10-Jul-12 20:49pm
v2

"\" is a escape character in C#. C# usually uses "\\" to denote "\". So, it is not a problem.
 
Share this answer
 
I assume you're looking into the variable (in Visual Studio).
I believe (not sure) visual studio displays the raw data and would displays a newline as '\n', thus display a slash as '\\'.
Makes sense, not?
 
Share this answer
 
v2
It is in single backslash format.
When you look at any string in the debugger, it shows them with appropriate escape characters: "\n", "\t" and "\\" for newline, tab and a single backslash respectively.

If you think about it, it makes sense: if it didn't you would not be able to tell any difference between
"Hello\nThere"
A single line with a backslash and a lower case 'n' between the words, and
"Hello\nThere"
Two lines, one of "Hello" and one of "There"
In the debugger, these would be shown as:
"Hello\\nThere"
and
"Hello\nThere"
respectively.
 
Share this answer
 
MIDL
string str1 = "123\\456\\789\\10";
string str2 = str1.Replace('\',"");
 
Share this answer
 
v2
Comments
Groulien 2-May-11 6:33am    
Won't compile, '' is not a character.
Use str1.Replace("\\", "") instead.
--Edit:--
Sorry my friend, but no overload exists for string.Replace(char, string).
Venkatesh Mookkan 3-May-11 1:40am    
Actually you should use "@" when you want to "\" directly like below,

str1.Replace(@"\","");
MIDL
 try this 
string strText = "as\\fsff\\sfff\\fsf\\";
                Response.Write(strText.Replace("\\",@"\"));
 
Share this answer
 
Comments
Groulien 2-May-11 7:14am    
The heck?
You're replacing one character with the same!
.Replace(@"\\",@"\")
kapil0411 2-May-11 7:15am    
Hi,
Thanks for your response. bt in this solution if i am lookion value in debuging mode it display in double slash....
try this, should work

C#
public string removeDoubleBackslashes(string input)
 {
     char[] separator = new char[1] { '\\' };
     string result = "";
     string[] subResult = input.Split(separator);
     for (int i = 0; i <= subResult.Length - 1; i++)
     {
         result = i < subResult.Length - 1 ? result + subResult[i] + "\\" : result + subResult[i];
     }
     return result;
 }
 
Share this answer
 
v3

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