0

Possible Duplicate:
Can I convert a C# string value to an escaped string literal

I have a small program where I want the user to be able to put in escape characters like "\n\r" "\t", etc. Do I HAVE to do a replace on these or is there a way that it just works with what the user puts in?

When the use passes a string like the following ("This is a \r\n test") in the command line, in the program it shows up as \r\n. I can replace it, but I'm curious if there is a way the user could enter these that the C# program could just interpret correctly without me having to do a replace on it?

3
  • \n and \r are recognized by the C# compiler and so they only apply to C# code. If you want your program to process them, you'll need to parse the user's input and replace the character with the appropriate codes. Commented Jan 31, 2013 at 15:34
  • His question was, "Does the .NET framework includes a method that automatically replace the escaped characters like the C# compiler do?". Commented Jan 31, 2013 at 15:39
  • @zmbq: That's the other way around. Commented Jan 31, 2013 at 15:45

2 Answers 2

2

You need to replace all codes manually, for example:

text = text.Replace(@"\r\n", Environment.NewLine);

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Bummer, was hoping there was a way the user could have entered it. Thanks anyway!
0

It is possible, I think, for the user to enter these directly, but the user would need to know how to accomplish this. There is nothing that you can code that just makes it happen, so you should do something like:

input.Replace("\\r", "\r");
input.Replace("\\n", "\n");

Just to make sure.

2 Comments

What would the replace be for \t?
The \ character will have to be escaped, so \t becomes \\t, or you can use the @ qualifier as Tim Schmelter suggested, that just removes the need to escape the ``.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.