views:

67

answers:

3

I can't get my head around quotes.

EG, I have a string for an mp3 with quotes:

string ARTIST = "John \"The Man\" Doe"

I then want to pass this to a command line WITH the escape sequences. I therefore need my string to look like (I think):

ARTIST = "John \\\"The Man\\\" Doe"

So it looks like, for every time I have an (actual) " in my string, I need \".

I have tried using:

ARTIST.Replace("\"","\\\"") 

But this has not worked. What is the correct way to handle quotes at the command line in my application?

A: 

The windows command line doesn't use \ as an escape for a quote, it uses a redundant quote,

so you want to send "John ""The Man"" Doe" which you get in C# by escaping each quote

string ARTIST = "John \"\"The Man\"\" Doe"
John Knoeller
thanks but the string I retrieve from elsewhere, it already comes as `"John \"The Man\" Doe"` (for example)
Dave
it is being fed into the LAME.exe mp3 encoder and the \ escape character works when i tried manually
Dave
The method of escaping quotes is application specific, but most applications use the \ escape characters (C#'s `Main(string[])` uses \ escape characters, for example).
Ruben
yes, but most command line parsers _dont_ because \ is the path separator in windows.
John Knoeller
If you read the docs on how `Environment.GetCommandLineArgs` works, you'll see it has some provisions on how this works. So only \" is treated as an escape, but C:\ is not. (IIRC this is the same for MS VC++.) See http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx
Ruben
A: 

The basic idea does seem to be correct, but did you also add the surrounding quotes, i.e.,

"\"" + ARTIST.Replace("\"", "\\\"") + "\""

Also, note that you'll need three backslashes rather than two to escape \"; the compiler would complain if you didn't.

Ruben
+2  A: 

Make sure you are doing:

ARTIST = ARTIST.Replace("\"","\\\"");

Instead of just:

ARTIST.Replace("\"","\\\"");

Is this the case already?

Nick Craver
lol thank you! d'oh! :)
Dave