string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
来源:互联网 发布:淘宝无线端装修工具 编辑:程序博客网 时间:2024/04/29 19:58
http://blog.csdn.net/csethcrm/article/details/37928353
由于原来一直都没注意到这两个方法,一直使用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相比,肯定是前面一个性能更高【没有测试过,如果有哪位测试过的可以留言告诉我哦,谢谢!】,所以还是要选择性使用的。
0 0
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace的区别
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
- String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()
- String.IsNullOrEmpty()和String.IsNullOrWhiteSpace() 百度编辑器
- string.IsNullOrEmpty and string.IsNullOrWhiteSpace
- string.IsNullOrEmpty与string.IsNullOrWhiteSpace
- 判断空值String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()
- String.IsNullorEmpty()方法的使用
- String.IsNullOrEmpty()方法的使用
- String.IsNullOrEmpty 方法
- String.IsNullOrEmpty 方法
- String.IsNullOrEmpty 方法
- String.IsNullOrEmpty方法
- C#的String类的IsNullOrEmpty方法
- 在无头单链表的一个非头节点前插入一个节点
- 通过包名和类名启动应用程序报错
- 链式二叉树的递归与遍历
- 机器学习相关资料推荐 http://blog.csdn.net/jiandanjinxin/article/details/51130271
- 前端页面常见js应用
- string.IsNullOrEmpty和string.IsNullOrWhiteSpace方法的区别
- 使用Unity3D 5.3.4 模仿2D游戏 FlappyBird(一)
- STL queue队列容器和priority_queue优先队列容器
- 每天laravel-20160727| Genaerator command-5
- java的编码原理
- java排序之快排
- Mac sublime text3 快捷键
- struts2标签库详解
- easyui datagrid客户端分页(7)