Search code examples
c#stringconsolecharconsole.writeline

Why does Console.WriteLine() function miss some characters within a string?


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.


Solution

  • (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