Replace double backslash \\ with single backslash \ in a string

  • 12 years ago

    hi all,

    How can i replace a double backslash with a single backslash. I am using the following code:

    string pathofbook = server.mappath("~");

    string s = pathofbook.replace("\\","\");

    But the above code gives me an error.....unrecognised escape parameter(i.e. for single backslash). I tried using : pathofbook.replace("\\", @"\") but it doesn't do anything. It returns the same string.

    How can i perform this replacement?



  • 12 years ago

     try pathofbook.replace(@"\\", @"\")

  • 12 years ago

    Hi try this, public static string FixupUrl(string Url)


    if (Url.StartsWith("~") )

    return (HttpContext.Current.Request.ApplicationPath +



  • 12 years ago


    Khan: the solution you have provided doesn't change the output. I mean, it returns the same string as input string.

    Sraja: as per your solution is concerned, you have used forward slash instead of black slash. The forward slash( / ) doesn't trouble you much. The problem occurs when you use back slash( \ ). It gives you an error like "Newline in Constant".




Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“If debugging is the process of removing software bugs, then programming must be the process of putting them in.” - Edsger Dijkstra