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?

Community
  • 1
  • 1
user441521
  • 6,942
  • 23
  • 88
  • 160
  • \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. – Pete Jan 31 '13 at 15:34
  • His question was, "Does the .NET framework includes a method that automatically replace the escaped characters like the C# compiler do?". – Cédric Bignon Jan 31 '13 at 15:39
  • @zmbq: That's the other way around. – Tim Schmelter Jan 31 '13 at 15:45

2 Answers2

2

You need to replace all codes manually, for example:

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

Demo

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
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.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65