String from byte array doesn't get trimmed in C#?

后端 未结 5 1800
南笙
南笙 2020-12-18 18:40

I have a byte array similar to this (16 bytes):

71 77 65 72 74 79 00 00 00 00 00 00 00 00 00 00

I use this to convert it to a string and tr

5条回答
  •  我在风中等你
    2020-12-18 18:52

    Trim by default removes only whitespace, where whitespace is defined by char.IsWhitespace.

    '\0' is a control character, not whitespace.

    You can specify which characters to trim using the Trim(char[]) overload:

    string result = Encoding.ASCII.GetString(data).Trim(new char[] { '\0' });
    

提交回复
热议问题