成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

C#截取字符串

2009-08-21 09:09:05

C#字符串

2009-08-06 16:01:09

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 17:06:37

C#字符串

2009-08-26 13:24:54

C#字符串

2009-08-24 13:04:44

操作步驟C#字符串

2009-08-07 13:50:11

C#字符串

2009-08-07 14:46:59

C#匹配字符串

2009-09-02 16:21:20

C#字符串

2009-08-28 10:39:37

C#數值字符串

2009-08-07 15:58:54

C#字符串插入html

2010-02-01 16:46:07

C++格式化字符串

2009-08-07 15:49:46

使用C#字符串

2009-08-06 17:24:08

C#字符串

2009-08-11 10:26:49

C#算法C#字符串反轉
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产成人精品一区二 | 成人影 | 久久综合九色综合欧美狠狠 | 在线观看 亚洲 | 久久精品国产免费看久久精品 | 天天插天天操 | 可以在线观看av的网站 | 亚洲精品久久久久久久久久吃药 | 精品国产欧美一区二区三区成人 | 夜夜艹 | 久久久精品一区二区三区 | 色婷婷亚洲国产女人的天堂 | 日韩精品中文字幕一区二区三区 | 亚洲3级 | 亚洲精品乱码久久久久久9色 | 乱一性一乱一交一视频a∨ 色爱av | 99久久视频| 黄色免费看 | 亚洲精品在线观看网站 | 国产精品日韩一区 | 亚洲欧美在线观看视频 | 亚洲国产精品福利 | 久久伊人一区二区 | 亚洲精品久久久一区二区三区 | 一本在线| а_天堂中文最新版地址 | 精品一区二区电影 | 久久里面有精品 | 久久久成人一区二区免费影院 | 国产一区久久 | 国产日韩欧美中文字幕 | 久久久国产精品视频 | 97视频久久 | 精品毛片在线观看 | 不卡的av电影 | www.精品一区 | 中文字幕亚洲一区二区三区 | 日本福利在线观看 | 久久99精品久久久久子伦 | 午夜亚洲 | 色综网 |