Why can't users put escape sequences in their input by default?
So I am working on this problem in which I have to take user input, check if it contains an escape sequence, and then escape.
My question is why the escapes are done on predefined string variables, but then you take the user's input and store it in a variable. This input contains an escape sequence such as \ n, but is not executed.
No user input Ex:
string noInput = "this is a escape \n sequence"
Console.WriteLine(noInput);
Console.ReadLine()
Output is : This is an escape
sequence
or user input Ex:
string input = Console.ReadLine();
Console.WriteLine(input);
Console.ReadLine();
Output is : This is an escape \n sequence
I hope I have explained my question well enough. I guess it might be for security reasons, but would like to know the answer.
source to share
The " escape sequence " is a language / compiler feature .. in this case C #. The corresponding language specification can be found at 2.4.4.5 String Literals
Note that the link refers to an older version of the language specification, but still applies. The latest version can be found here .
From the spec -
The character that follows the backslash character () in a regular string literal character must be one of the following characters: ',', \, 0, a, b, f, n, r, t, u, U, x , v. Otherwise, a compile-time error occurs. Example
- string a = "hello, world"; // hello world
- line b = @ "hello world"; // hello world
- line c = "hello \ t world"; // hello world
- line d = @ "hello \ t world"; // hello \ t world
The point is that the .Net language can dictate what special characters in a string literal will be treated as escape sequences. However, this is commonly used for centuries from languages ββlike C and C ++ in the old days.
When you accept user input. The entry (obviously?) Came as a literal string. (Another way to think, compiled .Net program is obviously compiler and language independent .. runtime aka CLR has no concept of escape sequences on strings)
If you want to provide functionality like this (maybe you have a good script). You have limited options.
- Use the following compiler functions like Roslyn to process the input string for you. I have never personally considered which specific API in Roslyn would help you with this, but it should be there, given that Roslyn should be...
Note that the con of this approach is that Roslyn can be quite cumbersome to include only one feature in your application.
- Write a small routine yourself that tries to accomplish the same as the compiler. For product quality code, this can be tricky (you have to understand and follow the spec to match it exactly .. and possibly update your version as this may change with future versions of C #. For example if a new escape sequence is introduced).
Although, practically speaking, the escape sequences in the C # spec shouldn't change willy-nilly .. but I wouldn't bet on it.
- Find a third party library that already does this for you (included for completeness of the answer.)
EDIT : Proof that the line you see (in the source code) is only an artifact of the source code in the given language -
Compile your C # application with the string "Hello \ nWorld". Open the compiled binary in a binary editor. The string you find in the compiled binary will be without the "\ n", replaced with the appropriate bytes for the newline character.
source to share
Try using substrings or any line manipulation to achieve what you want and get the / n part of the user input. Check it out here: http://msdn.microsoft.com/en-us/library/vstudio/ms228362(v=vs.110).aspx ;)
source to share