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. 从头开始构建LINUX[内核更新和资料]

    了解了LFS的内容后,至于内核的更新就一目了然了 http://www.cnblogs.com/2018/p/3979305.html http://www.cnblogs.com/2018/p/39 ...

  2. IOS开发基础知识--碎片3

    十二:判断设备 //设备名称 return [UIDevice currentDevice].name; //设备型号,只可得到是何设备,无法得到是第几代设备 return [UIDevice cur ...

  3. 【原】iOS:手把手教你发布代码到CocoaPods(Trunk方式)

    Change Log: 2015.08.20 - 添加podspec文件更新方法 2015.08.19 - 首次发布 概述 关于CocoaPods的介绍不在本文的主题范围内,如果你是iOS开发者却不知 ...

  4. Android客户端与Eclipse服务器端的Socket通信

    Server端代码 用来实现客户端socket的监听 package MyServer; import java.io.BufferedReader; import java.io.BufferedW ...

  5. iOS开发之功能模块--关于自定义TabBar条

    只上项目中用到的代码: 1.实现重写TabBar的TabBarItem,然后在中间额外加一个按钮. #import <UIKit/UIKit.h> @interface BikeTabBa ...

  6. python之ATM

    每次做一点就发出来,大神不要嫌重复 2016/11/4 今天来搞ATM,反正逃不了的,说来惭愧,这个作业是我10/4号20天前拿到的,当时是万脸蒙比的,今天又做了一点,现在算是百脸蒙比吧. 一.需求: ...

  7. Eclipse部署项目的原理简介eclipse,wtpwebapps,tomcat

    转载请注明出处! http://www.cnblogs.com/libingbin/ 感谢您的阅读.如果文章对您有用,那么请轻轻点个赞,以资鼓励.

  8. 偏移:translate ,旋转:rotate,缩放 scale,不知道什么东东:lineCap 实例

    <!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>canvas</ ...

  9. 基于nutz框架理解Ioc容器

    同样我们从问题入手去验证以及去理解Ioc容器都做了哪些事情: 1.nutz是有几种方式获取需要容器管理bean的信息? 第一种是使用json格式的文件进行配置,如: 第二种:使用注解@IocBean ...

  10. 基于redis分布式缓存实现(新浪微博案例)

    第一:Redis 是什么? Redis是基于内存.可持久化的日志型.Key-Value数据库 高性能存储系统,并提供多种语言的API. 第二:出现背景 数据结构(Data Structure)需求越来 ...