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 "    " </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 == ""都可以,这里面不讨论性能问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值