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

  • 9 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?

    Regards,

    Royal

  • 9 years ago

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

  • 9 years ago

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

    {

    if (Url.StartsWith("~") )

    return (HttpContext.Current.Request.ApplicationPath +

            Url.Substring(1)).Replace("//","/");

    }

  • 9 years ago

    Hi,

    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".

    Regards,

    Royal

     

Post a reply

Enter your message below

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

Contribute

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.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor