isnullorempty(string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别)

本文目录
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
- ASP.net中IsNullOrEmpty什么意思
- string.IsNullOrEmpty()是什么意思啊
string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
由于原来一直都没注意到这两个方法,一直使用string.IsNullOrEmpty,当看到string.IsNullOrWhiteSpace时,而且在微软人员开发的项目中经常使用时才注意到,查了一下MSDN,记一下免得以后忘记。
string.IsNullOrEmpty
都知道,这个功能是判断字符串是否为:null或者string.Empty。如果是如“\t“这样的字符就返回false了,为了达到判断过滤这些功能,就要使用Trim()和Length属性帮忙,判断是否长度为零,于是乎就产生了如下的方法。
string.IsNullOrWhiteSpace
这个是判断所有空白字符,功能相当于string.IsNullOrEmpty和str.Trim().Length总和,他将字符串给Char.IsWhiteSpace为ture的任何字符都将是正确的。根据MSDN的说明,这个方法会比调用上述两个方法的性能更高而且简洁,所以在判断这个功能时,推荐使用。
using System;
public class Example
{
public static void Main()
{
string values = { null, String.Empty, “ABCDE“,
new String(’ ’, 20), “ \t “,
new String(’\u2000’, 10) };
foreach (string value in values)
Console.WriteLine(String.IsNullOrWhiteSpace(value));
}
}
// The example displays the following output:
// True
// True
// False
// True
// True
// True
以上就是代码执行效果,至于性能就听微软的吧,不过string.IsNullOrEmpty和string.IsNullOrWhiteSpace相比,肯定是前面一个性能更高【所以还是要选择性使用的。
ASP.net中IsNullOrEmpty什么意思
whereLambda.where(c=》(companyName!=null)&&c.CompanyName.Contains(companyName)).where(c=》(!String.IsNullOrEmpty(regNumber))&&c.RegID.Contains(regNumber))差不多是这样
string.IsNullOrEmpty()是什么意思啊
string.IsNullOrEmpty()是一个判断字符串是否为空引用或者值为空的方法。
它能够同时判断一个String字符串是否是空引用(null),或值为空(empty),功能相当于string.IsNull()和string.Trim().Length总和。
string.IsNullOrEmpty()返回值是boolen型,也就是true或者false。
扩展资料
1、string.IsNullOrEmpty()的实现过程
string.IsNullOrEmpty(value)会判断传入的字符串参数value,当传入值value是空或者null,就返回true,否则返回false。
2、string.IsNullOrEmpty()在程序上的执行效率
string.IsNullOrEmpty()的执行效率要比string == null || string.Length == 0略低,但可读性更好。

更多文章:
易语言网页api接口怎么调用(易语言,怎么读取网页json的api)
2026年10月11日 08:00
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40
another time(another time和other time的区别)
2026年10月11日 05:00








