判定字符串为空方法(转)
来源:互联网 发布:福田网络布线szhxpc 编辑:程序博客网 时间:2024/06/09 18:33
首先明确””,null和string.Empty的区别:
string.Empty:不分配存储空间。
"":分配一个长度为空的存储空间 ,""和String.Empty,这两个都是表示空字符串,空字符串是一个特殊的字符串,只不过这个字符串的值为空,在内存中是有准确的指向的。
string.Empty就相当于"",一般用于字符串的初始化。比如: string a = string.Empty;在进行为空的比较时。string.Empty和""是一样的。即如果string test1 = "";则可以使用if(test1=="") 或者if(test1==string.Empty) 进行判断。上面两句是一样的效果。
Null:null 关键字是表示不引用任何对象的空引用的文字值。null 是引用类型变量的默认值。那么也只有引用型的变量可以为NULL,如果 int i=null,的话,是不可以的,因为Int是值类型的。
String.Empty和Null,这两个都是表示空字符串,string str1= String.Empty,这样定义后,str1是一个空字符串,空字符串是一个特殊的字符串,只不过这个字符串的值为空,在内存中是有准确的指向的 ,string str2=null,这样定义后,只是定义了一个string 类的引用,str2并没有指向任何地方,在使用前如果不实例化的话,都将报错。所以下面代码中执行test3.Length == 0就是错误的。
判断空字符串:
string test2 = string.Empty;
string test3 = null;
Response.Write("test1 = /"/"" +" ");
Response.Write("test2 = string.Empty" "</br>");
Response.Write("test3 = null" + "</br>");
if (test1 == "")
Response.Write("(test1 == /"/") is :True"+"</br>");
if(test2 == string.Empty)
Response.Write("(test2 == string.Empty) is:True" + "</br>");
if(test1 == string.Empty)
Response.Write("(test1 == string.Empty) is: True" + "</br>");
if(test2 == "")
Response.Write("(test2 == /"/") is: True" + "</br>");
if(test1 == test2)
Response.Write("(test1 == test2) is: True" + "</br>");
if(test3 == null)
Response.Write("(test3 == null) is: True" + "</br>");
if (test1 != null)
Response.Write("(test1 != null) is : True" + "</br>");
if (test2 != null)
Response.Write("(test2 != null) is : True" + "</br>");
if(test1.Length ==0)
Response.Write("(test1.Length ==0) is: True" + "</br>");
if(test2.Length==0)
Response.Write("(test2.Length==0) is : True" + "</br>");
//if(test3.Length == 0)//Error,null不能用Length来进行判断为空
if(string.IsNullOrEmpty(test1))
Response.Write("(string.IsNullOrEmpty(test1)) is :True" + "</br>");
if (string.IsNullOrEmpty(test2))
Response.Write("(string.IsNullOrEmpty(test2)) is :True" + "</br>");
if (string.IsNullOrEmpty(test3))
Response.Write("(string.IsNullOrEmpty(test3)) is :True" + "</br>");
输出:
test1 = ""
test2 = string.Empty
test3 = null
(test1 == "") is :True
(test2 == string.Empty) is:True
(test1 == string.Empty) is: True
(test2 == "") is: True
(test1 == test2) is: True
(test3 == null) is: True
(test1 != null) is : True
(test2 != null) is : True
(test1.Length ==0) is: True
(test2.Length==0) is : True
(string.IsNullOrEmpty(test1)) is :True
(string.IsNullOrEmpty(test2)) is :True
(string.IsNullOrEmpty(test3)) is :True
因此,判断字符串为空最通用的方法就是IsNullOrEmpty()无论是"", string.Empty还是null。如果字符串初始化为null,则不能使用test3.Length == 0进行判断。对于"",和string.Empty 使用s.Length == 0,s == string.Empty 和s == ""都可以,这里面不讨论性能问题。
- 判定字符串为空方法(转)
- String字符串判定是否为空方法总结
- Shell中判定字符串为空
- 判断字符串为空方法
- 【转】Java判断字符串是否为空方法
- 判断字符串是否为空的方法
- 判断字符串是否为空的方法
- 字符串为空判断最快方法
- IOS 判断字符串为空的方法
- 判断字符串为空的方法
- 判断字符串为空的方法
- java判断字符串为空的方法
- 判断字符串是否为空的方法
- 判断字符串为空的方法详解
- iOS判断字符串为空的方法
- 判断字符串不为空的方法
- iOS中判断字符串是否为空字符串的方法
- Java中各种“空”的判定方法
- jQuery 执行顺序
- Asp.net 单个和多个站点地图的使用
- Oracle11g中,设置主键为自增1形式-创建序列
- 连日枝头三首
- 防止页面在跳转的时候被SQL注入
- 判定字符串为空方法(转)
- db2/infomix/oralce/sqlserver选择指定条数语句
- 广告扇
- 系统集成管理(PMP常见疑难知识点)
- Lua 语言和C集成调研小结
- 关于23种设计模式的有趣见解
- 标签删除工具(V1.0.1)
- html 表格特殊标题 (legend)
- object的flash在firefox上显示不出来的问题