C# verbatim strings
Discussion
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).
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).
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"
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"
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"
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"
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
? 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
I'm not a c# pogrammer but if this is really happening
>> Edited by ATG on Tuesday 28th March 19:51
debugger said: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.
? a
@"C:Windows"
? b
"C:Windows"
>> Edited by ATG on Tuesday 28th March 19:51
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
). 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
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.
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.
Gassing Station | Computers, Gadgets & Stuff | Top of Page | What's New | My Stuff


