Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi,
i am trying to replace double backslash into single backslash.but its not happenuig correctly.
 
Suppose my string is
 
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 2-May-11 1:20am
Edited 10-Jul-12 21:49pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

"\" is a escape character in C#. C# usually uses "\\" to denote "\". So, it is not a problem.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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?
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

string str1 = "123\\456\\789\\10";
string str2 = str1.Replace('\',"");
  Permalink  
v2
Comments
nbgangsta at 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 at 3-May-11 1:40am
   
Actually you should use "@" when you want to "\" directly like below,
 
str1.Replace(@"\","");
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

 
 try this 
string strText = "as\\fsff\\sfff\\fsf\\";
                Response.Write(strText.Replace("\\",@"\"));
  Permalink  
Comments
nbgangsta at 2-May-11 7:14am
   
The heck?
You're replacing one character with the same!
.Replace(@"\\",@"\")
kapil0411 at 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....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

try this, should work
 
       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;   
        }
  Permalink  
v3

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 Afzaal Ahmad Zeeshan 204
4 DamithSL 194
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 11 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100