C# String Handling

  • Thread starter Thread starter Ryan Folstad
  • Start date Start date
R

Ryan Folstad

Hello i have a problem with c# string handling. Hopefully someone can
explain what im doing wrong.

this code:
string windowsdir = "c:\\windows";
string installdir ="c:\\install";
string path= String.Format("{0}\\system32\\cscript.exe {1}\\filename.vbs",
windowsdir, installdir);

The inspector shows:
path = @"c:\windows\system32\cscript.exe c:\install\filename.vbs"

Problem is when i add the escape characters for \" ie:
string path= String.Format("{0}\\system32\\cscript.exe
\"{1}\\filename.vbs\"", windowsdir, installdir);

The inspector now shows:
path = "c:\\windows\\system32\\cscript.exe \"c:\\install\\filename.vbs"

(No @ anymore and the escape sequences don't seem to be applied)

Can anyone explain what is going on here? Do i have the escape sequence for
a double quote wrong? Why is the addition of \" causing all escape
sequences to not seem to be applied?

FYI.. im using .net 1.1 on win2k3.

Thanks,

R
 
Problem is when i add the escape characters for \" ie:
string path= String.Format("{0}\\system32\\cscript.exe
\"{1}\\filename.vbs\"", windowsdir, installdir);

The inspector now shows:
path = "c:\\windows\\system32\\cscript.exe \"c:\\install\\filename.vbs"

(No @ anymore and the escape sequences don't seem to be applied)

Can anyone explain what is going on here? Do i have the escape sequence for
a double quote wrong? Why is the addition of \" causing all escape
sequences to not seem to be applied?

When you say "inspector" I assume you are referring to a watch window? I
had the same problem a bit ago. Your escape characters will still work when
you use the string. It just shows it with the escape characters in the
watch window so you won't confuse your \" with the double-quotes that it
surrounds strings with. For example:

string strFoo = "my \"quoted string\"";

would show up as:

strFoo = "my "quoted string""

in the watch window if they didn't leave in the escape characters. The
double "" can be confusing and hard to discern on some monitors. Also,
without the escaped double-quotes, it'd be hard to tell whether the
enclosing double-quotes were part of the string or just watch-window syntax.
 
yes i was referring to the watch window. And yes the presence of a \" was
simply causing the watch window to display the string without the escape
characters applied. Console.WriteLine shows the string as i expected it
should look.

Thanks for you help.

R
 
Can anyone explain what is going on here? Do i have the escape sequence for
a double quote wrong? Why is the addition of \" causing all escape
sequences to not seem to be applied?

I suspect it's just showing it in a different format - try printing the
value out to the console or a log file. I suspect you'll find it's
fine, it's just the watch window behaviour.
 
Back
Top