Reputation: 924
suppose there is a string created like: string s="This \n is new staring";
Above code we have got a \n which is a new line character which can be escaped either via an @ before the double quotes or by using \.
The confusion is my mind is that:
a. suppose this value is taken from a input box into the code then the \n get treated as normal string (automatically gets \n) and not new line character.
b. same if we assign the textbox value to a multiline textbox or label, the value gets treated as \n and is output as \n and not newline.
Is there any reference for this concept please.
Upvotes: 0
Views: 70
Reputation: 9782
The basics:
In memory you may have these bytes:
0x41, 0x42, 0x0a, 0x00
in decimal the same memory sells read as
65, 66, 10, 0
If interpteted as ASCII characters you have
'A', 'B', Newline, EndOfString
If you want to express these in source code, you may write:
string s = "AB\n"; // note: 0x00 is appended automatically
The compiler will do all the conversions and end up with the bytes in the first example. So "\n" is just a way to include the special chars Newline
/ Byte 0x0A in a string in a convenient way.
If you receive something from another application, through a file or a socket or whatever, you in fact receive Bytes, which your code may interpret in one of the ways explained above.
Please note: In .Net we have UTF strings, where each character in fact uses 16Bit/two Bytes, so the above explanation is good for C but not exactly true for C#. The basic principles are the same however.
Upvotes: 1
Reputation: 26446
The escape characters are processed at compile time. This means that the compiled version of your program does not contain any escape characters.
Same goes for the @""
notation: it just changes the way you can supply data for the string while writing your program, and is not included in your compiled program.
Data entered in a text box does not undergo escape character processing, so entering \n
really is a string containing the characters '\'
and 'n'
.
Upvotes: 1