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

后端 未结 7 2373
野的像风
野的像风 2021-02-12 10:42

I have a string, declared as:

 string text = \"THIS IS LINE ONE \"+(Char)(13)+\" this is line 2\";

And yet, When I write Console.WriteLin

7条回答
  •  余生分开走
    2021-02-12 11:37

    (Char)13 is a carriage return, while (Char)10 is a line feed. As others have said, this means that (Char)13 will return to the beginning of the line you are on, so that by the time you have written the (shorter) line 2, it will be written over the first section of the string - thus the remaining "E".

    If you try it with a shorter second string, you can see this happening:

    string text = "THIS IS LINE ONE " + (Char)13 +"test";
    Console.WriteLine(text);
    

    gives output:

    "test IS LINE ONE"
    

    Therefore, to solve your problem, the correct code will be:

    string text = "THIS IS LINE ONE " + (Char)10 +"test";
    Console.WriteLine(text);
    

    which outputs:

    THIS IS LINE ONE
    this is line 2
    

    Edit:

    Originally, since you said your printer requires it to be a (Char)13, I suggested trying both (Char)10 + (Char)13 (or vice versa) to achieve a similar effect. However, thanks to Peter M's exceedingly helpful comments, it appears the brand of printer you are using does in fact require just a (Char)10 - according to the manual, (Char)10 will produce a line feed and a carriage return, which is what you require.

提交回复
热议问题