Reputation: 2243
I want a String
to have a New Line
in it, but I cannot use escape sequences because the interface I am sending my string to does not recognize them. As far as I know, C# does not actually store a New Line
in the String
, but rather it stores the escape sequence, causing the literal contents to be passed, rather than what they actually mean.
My best guess is that I would have to somehow parse the number 10 (the decimal value of a New Line
according to the ASCII table) into ASCII. But I'm not sure how to do that, because C# parses numbers directly to String
if attempting this:
"hello" + 10 + "world"
Any suggestions?
Upvotes: 1
Views: 2460
Reputation: 55609
If you say "hello\nworld"
, the actual string will contain:
hello
world
There will be an actual new-line character in the string. At no point are the characters \
and n
stored in the string.
There are a few ways to get the exact same result, but a simple \n
in the string is a common way.
A simple cast should also do the same:
"hello" + (char)10 + "world"
Although likely slightly slower because of string concatenation. I say "likely" because it could probably be optimized away, or an actual example using \n
will also result in string concatenation, taking roughly the same amount of time.
Test.
Upvotes: 5
Reputation: 3526
The preferred new line character is Environment.NewLine
for its cross-platform capability.
Upvotes: 2
Reputation: 3109
You could use xml for communication, if you're receiver can handle this
Upvotes: 0