网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

C#中怎么判断字符串

C#中怎么判断字符串,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

“只有客户发展了,才有我们的生存与发展!”这是创新互联建站的服务宗旨!把网站当作互联网产品,产品思维更注重全局思维、需求分析和迭代思维,在网站建设中就是为了建设一个不仅审美在线,而且实用性极高的网站。创新互联对做网站、网站设计、网站制作、网站开发、网页设计、网站优化、网络推广、探索永无止境。

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就是错误的。

C#判断字符串应用之判断空字符串实例演示:

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﹥“);

C#判断字符串应用之判断空字符串实例输出:

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

因此,C#判断字符串应用为空最通用的方法就是IsNullOrEmpty()无论是”", string.Empty还是null。如果字符串初始化为null,则不能使用test3.Length == 0进行判断。对于”",和string.Empty 使用s.Length == 0,s == string.Empty 和s == “”都可以,这里面不讨论性能问题。

关于C#中怎么判断字符串问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


分享文章:C#中怎么判断字符串
当前地址:http://bjjierui.cn/article/igjejo.html

其他资讯