How do you remove the escape char \ from a string?

In my code I collect a few strings that I put together to run from the command line:

string softDemoArgs = string.Format("\"{0}\" \"{1}\" > \"{2}\"", softDemoPath, wav8KFileName, wav8KText);

This causes the softDemoArgs string to look like:

"\"C:\\Program Files\\AudioSoft\\AudioWord\\SOFTDEMO.EXE\" \"C:\\Program Files\\AudioSoft\\AudioWord\\TempWavs\\tempWav8K.wav\" > \"C:\\Program Files\\AudioSoft\\AudioWord\\TempWavs\\tempWav8K.txt\""

Unfortunately SOFTDEMO.EXE wont work with the escape chars! Is there any way of extracting them before starting the command process

What I need is:

"C:\Program Files\AudioSoft\AudioWord\SOFTDEMO.EXE" "C:\Program Files\AudioSoft\AudioWord\TempWavs\tempWav8K.wav" > "C:\Program Files\AudioSoft\AudioWord\TempWavs\tempWav8K.txt"

Any help would be great thanks,

Guy



Answer this question

How do you remove the escape char \ from a string?

  • Cory E

    If the path is being obtained as a path from the system, then by default the \ will always be escaped as a \\

    If you know you're always dealing with paths, then the simplest way is to use

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

    The reason for the @ before each string is so that it then doesn't try to treat each \ as a control character

    HTH

    -aj



  • sl0140

    The problem was that it was trying to use (1) as an argument and it couldn't find the file! But when I edited the string manually and removed the extra backslashes in the command window it worked fine!

    (1) "\"C:\\Program Files\\AudioSoft\\AudioWord\\TempWavs\\tempWav8K.wav\" > \"C:\\Program Files\\AudioSoft\\AudioWord\\TempWavs\\tempWav8K.txt\""

    Now what I'm doing is using (2) and then writing the output to a text file!

    (2)

    Process p = new Process();

    p = Process.Start(theStartInfo);

    string output = p.StandardOutput.ReadToEnd();

    Thanks for all your help!

    Guy


  • ssekhar

    String.Replace



    string s = @"C:\\Program Files\\AudioSoft\\AudioWord\\SOFTDEMO.EXE\";
    string b = s.Replace(@"\\", @"\");





  • kmcclung

    Why does wav8KFileName and wav8KText have the "\\" If you use a string literal to set them:

    string wav8KFile = "C:\\Program Files\\AudioSoft\\AudioWord\\TempWavs\\tempWav8K.wav";

    The result would be "C:\Program Files\AudioSoft\AudioWord\TempWavs\tempWav8K.wav"

    So where are the extra slashes in the strings coming from

    Escape characters only make sense in string literals, once put in a string variable, they're already escaped.



  • How do you remove the escape char \ from a string?