您的位置:首页 > 其它

string.IsNullOrEmpty和string.IsNullOrWhiteSpace的区别

2016-07-13 16:59 567 查看

string.IsNullOrEmpty和string.IsNullOrWhiteSpace

本人一直使用的是string.IsNullOrEmpty方法来判断字符串是否为空,今天偶然发现项目经理判断是写的string.IsNullOrWhiteSpace这个方法,由于好奇心网上查了一下这2个方法的区别,觉得区别还是挺大的,用事实说话:

string s1 = null;
string s2 = string.Empty;
string s3 = "";
string s4 = "  ";
string s5 = "\t";
Console.WriteLine(string.IsNullOrEmpty(s1) + "------" + string.IsNullOrWhiteSpace(s1));
Console.WriteLine(string.IsNullOrEmpty(s2) + "------" + string.IsNullOrWhiteSpace(s2));
Console.WriteLine(string.IsNullOrEmpty(s3) + "------" + string.IsNullOrWhiteSpace(s3));
Console.WriteLine(string.IsNullOrEmpty(s4) + "------" + string.IsNullOrWhiteSpace(s4));
Console.WriteLine(string.IsNullOrEmpty(s5) + "------" + string.IsNullOrWhiteSpace(s5));
Console.ReadKey();


输出结果  :



结论:string.IsNullOrEmpty方法无法判断空字符串和带有换行符的字符串,所以string.IsNullOrWhiteSpace方法的功能要更完善,通过查看MSDN



微软给的解释是:

                  string.IsNullOrWhiteSpace方法的性能要高,所以建议使用此方法,最后决定以后判断字符串为空都用这个方法了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: