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

后端 未结 9 734
广开言路
广开言路 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:22

    The differences in practice :

    string testString = "";
    Console.WriteLine(string.Format("IsNullOrEmpty : {0}", string.IsNullOrEmpty(testString)));
    Console.WriteLine(string.Format("IsNullOrWhiteSpace : {0}", string.IsNullOrWhiteSpace(testString)));
    Console.ReadKey();
    
    Result :
    IsNullOrEmpty : True
    IsNullOrWhiteSpace : True
    
    **************************************************************
    string testString = " MDS   ";
    
    IsNullOrEmpty : False
    IsNullOrWhiteSpace : False
    
    **************************************************************
    string testString = "   ";
    
    IsNullOrEmpty : False
    IsNullOrWhiteSpace : True
    
    **************************************************************
    string testString = string.Empty;
    
    IsNullOrEmpty : True
    IsNullOrWhiteSpace : True
    
    **************************************************************
    string testString = null;
    
    IsNullOrEmpty : True
    IsNullOrWhiteSpace : True
    

提交回复
热议问题