C#判断字符串应用详细解析

开发 后端
C#判断字符串应用是我们在编程过程中经常用到的,那么具体的C#判断字符串应用是什么呢?我们在C#判断字符串应用方面需要掌握什么呢?那么本文就向你详细介绍这方面的内容。

C#判断字符串应用是如何的呢?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#判断字符串应用之判断空字符串实例演示:

  1. string test1 = “”;  
  2. string test2 = string.Empty;  
  3. string test3 = null;  
  4. Response.Write(“test1 = \”\”“ +“ “);  
  5. Response.Write(“test2 = string.Empty“ “﹤/br﹥“);  
  6. Response.Write(“test3 = null“ + “﹤/br﹥“);  
  7. if (test1 == “”)  
  8. Response.Write(“(test1 == \”\”) is :True“+“﹤/br﹥“);  
  9. if(test2 == string.Empty)  
  10. Response.Write(  
  11. “(test2 == string.Empty) is:True“ + “﹤/br﹥“);  
  12.  
  13. if(test1 == string.Empty)  
  14. Response.Write(  
  15. “(test1 == string.Empty) is: True“ + “﹤/br﹥“);  
  16. if(test2 == “”)  
  17. Response.Write(  
  18. “(test2 == \”\”) is: True“ + “﹤/br﹥“);  
  19.  
  20. if(test1 == test2)  
  21. Response.Write(  
  22. “(test1 == test2) is: True“ + “﹤/br﹥“);  
  23.  
  24. if(test3 == null)  
  25. Response.Write(  
  26. “(test3 == nullis: True“ + “﹤/br﹥“);  
  27.  
  28. if (test1 != null)  
  29. Response.Write(  
  30. “(test1 != nullis : True“ + “﹤/br﹥“);  
  31.  
  32. if (test2 != null)  
  33. Response.Write(  
  34. “(test2 != nullis : True“ + “﹤/br﹥“);  
  35.  
  36. if(test1.Length ==0)  
  37. Response.Write(  
  38. “(test1.Length ==0) is: True“ + “﹤/br﹥“);  
  39.  
  40. if(test2.Length==0)  
  41. Response.Write(  
  42. “(test2.Length==0) is : True“ + “﹤/br﹥“);  
  43. //if(test3.Length == 0)//Error,null不能用Length来进行判断为空  
  44. if(string.IsNullOrEmpty(test1))  
  45.  
  46. Response.Write(  
  47. “(string.IsNullOrEmpty(test1)) is :True“ + “﹤/br﹥“);  
  48. if (string.IsNullOrEmpty(test2))  
  49.  
  50. Response.Write(  
  51. “(string.IsNullOrEmpty(test2)) is :True“ + “﹤/br﹥“);  
  52. if (string.IsNullOrEmpty(test3))  
  53.  
  54. Response.Write(  
  55. “(string.IsNullOrEmpty(test3)) is :True“ + “﹤/br﹥“);  

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

  1. test1 = “”  
  2. test2 = string.Empty  
  3. test3 = null 
  4. (test1 == “”) is :True  
  5. (test2 == string.Empty) is:True  
  6. (test1 == string.Empty) is: True  
  7. (test2 == “”) is: True  
  8. (test1 == test2) is: True  
  9. (test3 == nullis: True  
  10. (test1 != nullis : True  
  11. (test2 != nullis : True  
  12. (test1.Length ==0) is: True  
  13. (test2.Length==0) is : True  
  14. (string.IsNullOrEmpty(test1)) is :True  
  15. (string.IsNullOrEmpty(test2)) is :True  
  16. (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#判断字符串应用有所帮助。

【编辑推荐】

  1. C#动态二维数组函数处理方案
  2. C#集合、C#动态数组的概念浅析
  3. C#动态数组的详解介绍
  4. C#动态数组的应用详解实例
  5. C#数组复制方法详解
责任编辑:仲衡 来源: 互联网
相关推荐

2009-09-01 17:41:53

C#截取字符串函数

2009-09-01 17:58:55

C#截取字符串

2009-08-21 09:09:05

C#字符串

2009-09-01 17:50:23

C#截取字符串

2009-08-07 14:22:56

C#字符串搜索

2009-08-24 17:06:37

C#字符串

2009-08-07 13:50:11

C#字符串

2009-08-06 16:01:09

C#字符串函数大全

2009-08-07 14:34:33

C#模式字符串

2009-08-26 13:24:54

C#字符串

2009-08-24 13:04:44

操作步骤C#字符串

2009-08-07 14:15:21

C#字符串分割

2009-08-07 14:46:59

C#匹配字符串

2009-09-02 16:21:20

C#字符串

2010-02-01 16:46:07

C++格式化字符串

2009-08-28 10:39:37

C#数值字符串

2009-08-07 15:58:54

C#字符串插入html

2009-08-06 17:24:08

C#字符串

2009-08-07 15:49:46

使用C#字符串

2010-01-08 15:11:22

VB.NET字符串转义
点赞
收藏

51CTO技术栈公众号