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

后端 未结 7 2371
野的像风
野的像风 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:22

    The output isn't what it seems. Your terminal program is hiding you some characters of the bytes written in stdout because he interprets the carriage return presence as a text layout command (which is, go back to left). A hexdump can confirm that the output is correct by showing the very bytes of output.

    using System;
    public class Hello1 {
      public static void Main()  {
         string text = "THIS IS LINE ONE "+(Char)(13)+" this is line 2";
         System.Console.WriteLine(text);
       }   
    }
    

    Compile and run :

    $ gmcs a.cs
    $ ./a.exe
     this is line 2E
    $ ./a.exe | hexdump -C
    00000000  54 48 49 53 20 49 53 20  4c 49 4e 45 20 4f 4e 45  |THIS IS LINE ONE|
    00000010  20 0d 20 74 68 69 73 20  69 73 20 6c 69 6e 65 20  | . this is line |
    00000020  32 0a                                             |2.|
    00000022
    

提交回复
热议问题