C#中怎么判断空字符串_第1页
C#中怎么判断空字符串_第2页
C#中怎么判断空字符串_第3页
C#中怎么判断空字符串_第4页
C#中怎么判断空字符串_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C#中怎么判断空字符串 首先明确”,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 test1 = ;string test2 = string.Empty;string test3 = null;Response.Write(test1 = + );Response.Write(test2 = string.Empty);Response.Write(test3 = null + );if (test1 = )Response.Write(test1 = ) is :True+);if(test2 = string.Empty)Response.Write(test2 = string.Empty) is:True + );if(test1 = string.Empty)Response.Write(test1 = string.Empty) is: True + );if(test2 = )Response.Write(test2 = ) is: True + );if(test1 = test2)Response.Write(test1 = test2) is: True + );if(test3 = null)Response.Write(test3 = null) is: True + );if (test1 != null)Response.Write(test1 != null) is : True + );if (test2 != null)Response.Write(test2 != null) is : True + );if(test1.Length =0)Response.Write(test1.Length =0) is: True + );if(test2.Length=0)Response.Write(test2.Length=0) is : True + );/if(test3.Length = 0)/Error,null不能用Length来进行判断为空if(string.IsNullOrEmpty(test1)Response.Write(string.IsNullOrEmpty(test1) is :True + );if (string.IsNullOrEmpty(test2)Response.Write(string.IsNullOrEmpty(test2) is :True + );if (string.IsNullOrEmpty(test3)Response.Write(string.IsNullOrEmpty(test3) is :True + );输出:test1 = test2 = string.Emptytest3 = 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.Lengt

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论