Why string.IsNullOrWhiteSpace(“\0”) is false

前端 未结 6 1816
不思量自难忘°
不思量自难忘° 2021-01-18 13:17

I faced a problem where invisible character \\0 which is pretty like a \'white space\' not considered as white space by the string.IsNullOrWhiteSpace method. I

6条回答
  •  盖世英雄少女心
    2021-01-18 13:38

    '\0' character is not considered white space. See Char.IsWhitespace() for the list of characters that are considered white space.

    Use Enumerable.All() if you have your own requirements, or even just to add a few chars of your own. Something like this:

    bool IsMyKindOfWhiteSpace(string input)
    {
        char[] more = new char[] {  };
    
        return input.All(x => Char.IsWhiteSpace(x) || more.Contains(x));
    }
    

提交回复
热议问题