自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#判斷字符串應(yīng)用詳細(xì)解析

開發(fā) 后端
C#判斷字符串應(yīng)用是我們在編程過程中經(jīng)常用到的,那么具體的C#判斷字符串應(yīng)用是什么呢?我們在C#判斷字符串應(yīng)用方面需要掌握什么呢?那么本文就向你詳細(xì)介紹這方面的內(nèi)容。

C#判斷字符串應(yīng)用是如何的呢?C#判斷空字符串的操作需要注意和掌握的方面是什么呢?這是我們在實際開發(fā)的過程碰到的,那么我么看看C#判斷空字符串的操作具體的細(xì)節(jié):

C#判斷字符串應(yīng)用之判斷空字符串,首先明確””,null和string.Empty的區(qū)別:

string.Empty:

不分配存儲空間。

“”:

分配一個長度為空的存儲空間 ,”"和String.Empty,這兩個都是表示空字符串,空字符串是一個特殊的字符串,只不過這個字符串的值為空,在內(nèi)存中是有準(zhǔn)確的指向的。

string.Empty就相當(dāng)于”",一般用于字符串的初始化。比如: string a = string.Empty;在進(jìn)行為空的比較時。string.Empty和”"是一樣的。即如果string test1 = “”;則可以使用if(test1==”") 或者if(test1==string.Empty) 進(jìn)行判斷。上面兩句是一樣的效果。

Null:

null 關(guān)鍵字是表示不引用任何對象的空引用的文字值。null 是引用類型變量的默認(rèn)值。那么也只有引用型的變量可以為NULL,如果 int i=null,的話,是不可以的,因為Int是值類型的。

String.Empty和Null,這兩個都是表示空字符串,string str1= String.Empty,這樣定義后,str1是一個空字符串,空字符串是一個特殊的字符串,只不過這個字符串的值為空,在內(nèi)存中是有準(zhǔn)確的指向的 ,string str2=null,這樣定義后,只是定義了一個string 類的引用,str2并沒有指向任何地方,在使用前如果不實例化的話,都將報錯。所以下面代碼中執(zhí)行test3.Length == 0就是錯誤的。

C#判斷字符串應(yīng)用之判斷空字符串實例演示:

  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來進(jìn)行判斷為空  
  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#判斷字符串應(yīng)用之判斷空字符串實例輸出:

  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#判斷字符串應(yīng)用為空最通用的方法就是IsNullOrEmpty()無論是”", string.Empty還是null。如果字符串初始化為null,則不能使用test3.Length == 0進(jìn)行判斷。對于”",和string.Empty 使用s.Length == 0,s == string.Empty 和s == “”都可以,這里面不討論性能問題。

C#判斷字符串應(yīng)用相關(guān)的內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C#判斷字符串應(yīng)用有所幫助。

【編輯推薦】

  1. C#動態(tài)二維數(shù)組函數(shù)處理方案
  2. C#集合、C#動態(tài)數(shù)組的概念淺析
  3. C#動態(tài)數(shù)組的詳解介紹
  4. C#動態(tài)數(shù)組的應(yīng)用詳解實例
  5. C#數(shù)組復(fù)制方法詳解
責(zé)任編輯:仲衡 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2009-09-01 17:58:55

C#截取字符串

2009-09-01 17:41:53

C#截取字符串函數(shù)

2009-09-01 17:50:23

C#截取字符串

2009-08-21 09:09:05

C#字符串

2009-08-07 14:46:59

C#匹配字符串

2009-08-26 13:24:54

C#字符串

2009-08-24 17:06:37

C#字符串

2009-08-07 14:15:21

C#字符串分割

2009-08-07 14:22:56

C#字符串搜索

2009-08-07 14:34:33

C#模式字符串

2009-08-24 13:04:44

操作步驟C#字符串

2009-08-07 13:50:11

C#字符串

2009-08-06 16:01:09

C#字符串函數(shù)大全

2009-08-28 10:39:37

C#數(shù)值字符串

2009-08-07 15:58:54

C#字符串插入html

2009-09-02 16:21:20

C#字符串

2010-02-01 16:46:07

C++格式化字符串

2009-08-07 14:02:12

C#數(shù)據(jù)庫連接字符串

2009-08-11 10:26:49

C#算法C#字符串反轉(zhuǎn)

2009-08-21 15:06:09

C#連接字符串
點贊
收藏

51CTO技術(shù)棧公眾號