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#判断字符串应用之判断空字符串实例演示:

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 == nullis: True“ + “﹤/br﹥“);  
 
if (test1 != null)  
Response.Write(  
“(test1 != nullis : True“ + “﹤/br﹥“);  
 
if (test2 != null)  
Response.Write(  
“(test2 != nullis : 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﹥“);  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.

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 == nullis: True  
(test1 != nullis : True  
(test2 != nullis : 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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

因此,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-09-01 17:50:23

C#截取字符串

2009-08-21 09:09:05

C#字符串

2009-08-26 13:24:54

C#字符串

2009-08-06 16:01:09

C#字符串函数大全

2009-08-07 14:34:33

C#模式字符串

2009-08-07 14:15:21

C#字符串分割

2009-08-07 14:22:56

C#字符串搜索

2009-08-07 13:50:11

C#字符串

2009-08-24 13:04:44

操作步骤C#字符串

2009-08-24 17:06:37

C#字符串

2009-08-07 14:46:59

C#匹配字符串

2010-02-01 16:46:07

C++格式化字符串

2009-09-02 16:21:20

C#字符串

2009-08-28 10:39:37

C#数值字符串

2009-08-07 15:58:54

C#字符串插入html

2009-08-11 10:26:49

C#算法C#字符串反转

2009-09-04 10:26:09

Java和C#字符串类

2009-08-21 15:06:09

C#连接字符串
点赞
收藏

51CTO技术栈公众号