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(' ', ), " \t ",
new String('\u2000', ) };
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方法的区别的更多相关文章

  1. 【转载】C#中string.IsNullOrEmpty和string.IsNullOrWhiteSpace区别

    在C#中判断字段是否为空或者Null的时候,我们一般会使用到string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法,这两个方法在大部分情况下判断的结果是一致的 ...

  2. C#、.Net代码精简优化(空操作符(??)、as、string.IsNullOrEmpty() 、 string.IsNullOrWhiteSpace()、string.Equals()、System.IO.Path 的用法)

    一.空操作符(??)在程序中经常会遇到对字符串或是对象判断null的操作,如果为null则给空值或是一个指定的值.通常我们会这样来处理: .string name = value; if (name ...

  3. String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()的区别

    string.IsNullOrEmpty 这个是判断字符串是否为:null或者string.Empty或者“”,但不包含空格 .如果是如"\t"或者“   ” 这样的字符就返回fa ...

  4. String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()

    转自:http://hi.baidu.com/saclrpqmttbntyq/item/4592fc72c5a19e5c0d0a07eb 由于总用 String.IsNullOrEmpty( s ) ...

  5. String的几种初始化方法的区别

    参考了: java中String的两种初始化方法   String a; String aa = ""; String aaa = "123"; String ...

  6. String.IsNullOrEmpty 与 String.IsNullOrWhiteSpace

    String.IsNullOrEmpty 指示指定的字符串是否为 null 或者 空字符串: 返回值:如果参数为 null 或者 空字符串("" .String.Empty),结果 ...

  7. 1,字符是否为空,2,比较两个字符大小。String.Compare(String, String)。string.IsNullOrEmpty(string)

    1, String.Compare 方法 (String, String) 比较两个指定的 String 对象. 值 条件 小于零 strA 小于 strB. 零 strA 等于 strB. 大于零 ...

  8. String.IsNullOrWhiteSpace和String.IsNullOrEmpty的区别

    以前刚入行的时候判断字符串的时候用 string a="a"; a==""; a==null; 后来发现了String.IsNullOrEmpty感觉方便了好多 ...

  9. 【源码分析】你必须知道的string.IsNullOrEmpty && string.IsNullOrWhiteSpace

    写在前面 之前自信撸码时踩了一次小坑,代码如下: private static void AppServer_NewMessageReceived(WebSocketSession session, ...

随机推荐

  1. reason: Attempted to dereference an invalid ObjC Object or send it an unrecognized selector.

     album = responseObject[@"album"]; 是我将一个字典直接赋值给了对象 改为如下即可 [album setValuesForKeysWithDicti ...

  2. js获取网页高度

    网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.body.offsetWi ...

  3. LruCache缓存

    LruCache通常用于实现内存缓存,采用的缓存算法是LRU(Least Recently Used)即近期最少使用算法,其核心思想是:当缓存满的时候,会优先淘汰那些近期最少使用的缓存对象. 1.Lr ...

  4. 在xib中用KVC修改控件属性

    比如我们想在xib 中设置按钮的圆角,这样的话我们就可以在xib文件中进行修改,具体操作如下 实现效果,如下

  5. 初学RunLoop

    RunLoop 运行循环,跑圈 可以看出每条线程都有一个与之对应的RunLoop对象 主线程的RunLoop已经自动创建好了,子线程的RunLoop需要主动创建. 基本作用:保持程序的持续运行 处理A ...

  6. sql 截取日期

    截取日期: select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'yyyy-mm')from dual; 或得年或月或日   Year/ month/Da ...

  7. iOS多线程到底不安全在哪里?

    iOS多线程安全的概念在很多地方都会遇到,为什么不安全,不安全又该怎么去定义,其实是个值得深究的话题. 共享状态,多线程共同访问某个对象的property,在iOS编程里是很普遍的使用场景,我们就从P ...

  8. Linux From Scratch(从零开始构建Linux系统,简称LFS)- Version 7.7(三)

    八. 构建LFS系统 1. 准备虚拟内核文件系统 内核会挂载几个文件系统用于自己和用户空间程序交换信息.这些文件系统是虚拟的,并不占用实际磁盘空间, 它们的内容会放在内存里. mkdir -pv $L ...

  9. ios合并静态库

    lipo -create SQY/iOS/iphoneos/libGamePlusAPI.a SQY/iOS/iphonesimulator/libGamePlusAPI.a -output SQY/ ...

  10. 虚拟机linux上网问题

    VMware中虚拟机网络的三种设置 1.桥接(bridged)(设置上网比较繁琐,但是在嵌入式开发中比较有用)2.NAT(上网比较简单,但是不能用于嵌入式开发中)3.Host only (该模式下仅主 ...