newbie trying to understand escape characters in strings

  • Thread starter Thread starter teds
  • Start date Start date
T

teds

Hi,

I have the following simple code:

string foo1 = "My\rLine\rIs\rHere\r";
string foo2 = @"My\rLine\rIs\rHere\r";

When I do a Console.WriteLine() on each variable, the foo2 is displayed
with the \r displayed but foo1 is garbled. Why is that?

Also, suppose I'm given a string with \r used as a token separator
in a string variable. I'd to display it to the screen but it's mangled
in the same way as my first example. How can I convert
it to display correctly?

Thanks,

Ted
 
Hi Ted,

In the first example, are you wanting a newline between words, like this:

string foo1 = "My\nLine\nIs\nHere\n";

The second example is a verbatim string literal, prefixed with '@', which
ignores escape characters on purpose. It helps make things like paths,
"C:\\dir\\dir\\file.txt", easier to read "C:\dir\dir\file.txt".

Joe
 
I have the following simple code:

string foo1 = "My\rLine\rIs\rHere\r";
string foo2 = @"My\rLine\rIs\rHere\r";

When I do a Console.WriteLine() on each variable, the foo2 is displayed
with the \r displayed but foo1 is garbled. Why is that?

foo1 is "My" followed by a carriage return, followed by "Line" etc. The
result of a carriage return on the normal console is to go to the start
of the line, so you end up seeing just "Here".
Also, suppose I'm given a string with \r used as a token separator
in a string variable. I'd to display it to the screen but it's mangled
in the same way as my first example. How can I convert
it to display correctly?

What do you mean by "I'd like to display it to the screen"? Display
what, exactly, to the screen? "\r" has nothing *to* display on the
screen - it's just a carriage return.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top