I have a string, declared as:
string text = "THIS IS LINE ONE "+(Char)(13)+" this is line 2";
And yet, When I write Console.WriteLine(text);
,
the output is:
this is line 2E
Why is this behaviour happening? Or is it because I'm being stupid and missing something obvious?
Why does it not print:
THIS IS LINE ONE [CR] //where the CR is a non printed character this is line 2
EDIT
Please note: this is NOT a 'how do I add a carriage return' question.
WriteLine(String, Object, Object) Writes the text representation of the specified objects, followed by the current line terminator, to the standard output stream using the specified format information.
Yes it blocks. And there are no async console writes built into the framework that I know of.
The only difference between the Write() and WriteLine() is that Console. Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line.
First, Console. WriteLine() writes to the standard output stream which can be read by attaching a console window or through other means of intercepting the stream. Then, to send your output to a file you could setup a TraceListener which could be a file or something.
(char)13
is a carriage return (To the left margin on the current line)
THIS IS LINE ONE \r this is line 2"
Is interpreted as:
Print THIS IS LINE ONE
then *return* and print this is line 2
The overlap is: E
So you see: this is line 2E
This is how standard output on the console behaves.
"\n"
((Char)10
) will move the caret to the start of the next line"\r"
((Char)13
) will move the caret to the start of the current lineAs such, the results are thus the first line overwritten by the second line, leaving only the extra characters that the second line couldn't overwrite.
Since you've clarified that the string/characters have to be like that to get the behavior you want when this text is ultimately sent to the place you actually want to send it to, a dot matrix printer, then you need to test with the printer itself.
The above behavior is localized to the standard console. If you output the same characters to "standard output" but have redirected this to a printer, it is the printer's definition on how to deal with these characters that is important, and this may be different from the standard console.
As such, the behavior you see on the standard console is just that, the behavior of the standard console. You need to actually test with the printer to see how this behaves.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With