string.IsNullOrEmpty(string) vs. string.IsNullOrWhiteSpace(string)

后端 未结 9 718
广开言路
广开言路 2020-11-28 03:02

Is use of string.IsNullOrEmpty(string) when checking a string considered as bad practice when there is string.IsNullOrWhiteSpace(string) in .NET 4.

9条回答
  •  一向
    一向 (楼主)
    2020-11-28 03:24

    In the .Net standard 2.0:

    string.IsNullOrEmpty(): Indicates whether the specified string is null or an Empty string.

    Console.WriteLine(string.IsNullOrEmpty(null));           // True
    Console.WriteLine(string.IsNullOrEmpty(""));             // True
    Console.WriteLine(string.IsNullOrEmpty(" "));            // False
    Console.WriteLine(string.IsNullOrEmpty("  "));           // False
    

    string.IsNullOrWhiteSpace(): Indicates whether a specified string is null, empty, or consists only of white-space characters.

    Console.WriteLine(string.IsNullOrWhiteSpace(null));     // True
    Console.WriteLine(string.IsNullOrWhiteSpace(""));       // True
    Console.WriteLine(string.IsNullOrWhiteSpace(" "));      // True
    Console.WriteLine(string.IsNullOrWhiteSpace("  "));     // True
    

提交回复
热议问题