温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#中怎么判断字符串

发布时间:2021-07-08 14:59:56 来源:亿速云 阅读:448 作者:Leah 栏目:编程语言

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#中怎么判断字符串问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI