C# verbatim strings
Author
Discussion

Alex

Original Poster:

9,978 posts

314 months

Tuesday 28th March 2006
quotequote all
I have a regular string variable that I want to display in the Command Window as verbatim, so I can copy-and-paste it into a cmd window for testing. I can't find out how to do this!

e.g.:

string a=@"C:\Windows";
string b="C:\\Windows";

? a
"C:\Windows"

? b
"C:\\Windows"

? @b
error: managed EE does not understand expression's syntax

How can I print b so it gives "C:\Windows" (i.e. verbatim version).

pdV6

16,442 posts

291 months

Tuesday 28th March 2006
quotequote all
I'm new to this game (C#) but to me your strings a and b look the same.

The extra "\" in string b escapes the "\" that you want and the "@" in front of string a tells the compiler to treat the escape character as verbatim.

Therefore, I reckon "?a" and "?b" should give you the same output, i.e. "c:\windows"

pdV6

16,442 posts

291 months

Tuesday 28th March 2006
quotequote all
Having said that, I re-read your post and determine that you've tried that and it didn't work!

D'oh!

gopher

5,160 posts

289 months

Tuesday 28th March 2006
quotequote all
I can't see that you're doing anything wrong, in fact you could use both a or b, I even did a test

string a=@"C:\Windows";
string b="C:\\Windows";

Console.WriteLine(a);
Console.WriteLine(b);
Console.ReadLine();

and on running it got the following (expected) output

C:\Windows
C:\Windows

and as a double check queried the values in the command window in debug

? a
@"C:\Windows"

?b
@"C:\Windows"

Alex

Original Poster:

9,978 posts

314 months

Tuesday 28th March 2006
quotequote all
This is what I get in the debug window:

? a
@"C:\Windows"

? b
"C:\\Windows"

I want to output b to the debug window, so I get:
"C:\Windows"
or
@"C:\Windows"

The actual variable is very long with lots of escape characters (it's a C++ compiler call), I just want an easy way to get the verbatim version, so I can test it in a DOS window.



>> Edited by Alex on Tuesday 28th March 19:30

>> Edited by Alex on Tuesday 28th March 19:30

ATG

23,811 posts

302 months

Tuesday 28th March 2006
quotequote all
I'm not a c# pogrammer but if this is really happening
debugger said:

? a
@"C:Windows"

? b
"C:Windows"
then it suggests that the assignment = @"\" is not quite the same thing as = "\\" ... possible but a bit mad. Either the development environment has magically remembered how a and b's values were set, or a and b aren't of the same "type" (perhaps by both being "string" objects, but having some "literal" property set differently?). "? a" is probably calling a representation method of "a" that is designed to format it's output so its suitable for developers. Try using a string formatting expression ... something like ? "%s" % a or whatever the C# equivalent syntax is.

>> Edited by ATG on Tuesday 28th March 19:51

gopher

5,160 posts

289 months

Tuesday 28th March 2006
quotequote all
I think ATG is correct, and I think it is only VS that is treating them differently in debug mode and nothing else (it’s also odd that we get different outputs in debug tho' ).

What will your resultant output be? Try an output to that type and see what you get rather than rely on debug in VS

Cheers

Paul

>> Edited by gopher on Wednesday 29th March 08:13

slapmatt

1,132 posts

252 months

Wednesday 29th March 2006
quotequote all
I'm not sure what you're getting at, but the @ sign outputs the string, as you say, verbatim, whereas not having an @ sign means you have to use escape characters.

I tried:

string a=@"C:Windows";
string b="C:\Windows";
string c=@"C:\Windows";

Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
Console.ReadLine();

in a console app and got the output:

C:Windows
C:Windows
C:\Windows

Exactly what I'd expect TBH.

ATG

23,811 posts

302 months

Wednesday 29th March 2006
quotequote all
I imagine the difference is between using "?" and Console.WriteLine()