QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

泡泡马甲APP 更多内容请下载泡泡马甲手机客户端APP 立即下载 ×
查看: 2075|回复: 0

[C#] 为什么高手都是用IsNullOrWhiteSpace对字符串判空

[复制链接]

等级头衔

积分成就    金币 : 2806
   泡泡 : 1516
   精华 : 6
   在线时间 : 1244 小时
   最后登录 : 2024-5-5

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2022-10-15 16:13:07 | 显示全部楼层 |阅读模式
判断字符串为空有好几种方法:
% a( n7 S7 K9 y; D7 J8 e5 {方法一: 代码如下:% b, t: }. O1 p2 P* y: R" O
  1.   static void Main(string[] args)
  2.         {
  3.             string str = "";
  4.             if (str == "")
  5.             {
  6.                 Console.WriteLine("a is  empty"); ;
  7.             }
  8.             Console.ReadKey();
  9.         }
运行结果:a is empty3 I5 q  b, P; ]2 @) W, j5 B$ y- h
1.jpg

" M! {% K1 K4 m* a! Z这样针对str = ""也是可以的,但是大多数场景是在方法的 入口处判空,这个字符串有可能是null,也有可能是"   ",甚至是"\n",上面这种判空方法显示不能覆盖这么多场景;
- m, d$ v$ p2 C  E方法二 :这时候IsNullOrEmpty就横空出世了,针对字符串值为string.Empty、str2 = ""、null,都可以用  Y7 R# _9 c; G" [* G% |' e
  1.   static void Main(string[] args)
  2.         {
  3.             string str1 = string.Empty;
  4.             if (string.IsNullOrEmpty(str1))
  5.             {
  6.                 Console.WriteLine("str1 is  empty"); ;
  7.             }
  8.             string str2 = "";
  9.             if (string.IsNullOrEmpty(str2))
  10.             {
  11.                 Console.WriteLine("str2 is  empty"); ;
  12.             }
  13.             string str3 = null;
  14.             if (string.IsNullOrEmpty(str3))
  15.             {
  16.                 Console.WriteLine("str3 is  empty"); ;
  17.             }
  18.             Console.ReadKey();
  19.         }
运行结果如下:
, I  ^/ M$ [  ]; }; u 2.jpg

( V. ^9 _+ {9 [/ q8 w) v方法三 :但是IsNullOrEmpty在字符串为"     ","\n","\t",时候就无能为力了,为了覆盖这些场景,高手们一般判空使用方法IsNullOrWhiteSpace( v) _& r: i, V; P
  1. static void Main(string[] args)
  2.         {
  3.             string str1 = string.Empty;
  4.             if (string.IsNullOrWhiteSpace(str1))
  5.             {
  6.                 Console.WriteLine("str1 is  empty"); ;
  7.             }
  8.             string str2 = "";
  9.             if (string.IsNullOrWhiteSpace(str2))
  10.             {
  11.                 Console.WriteLine("str2 is  empty"); ;
  12.             }
  13.             string str3 = null;
  14.             if (string.IsNullOrWhiteSpace(str3))
  15.             {
  16.                 Console.WriteLine("str3 is  empty"); ;
  17.             }
  18.             string str4 = "    ";
  19.             if (string.IsNullOrWhiteSpace(str4))
  20.             {
  21.                 Console.WriteLine("str4 is  empty"); ;
  22.             }
  23.             string str5 = "\n";
  24.             if (string.IsNullOrWhiteSpace(str5))
  25.             {
  26.                 Console.WriteLine("str5 is  empty"); ;
  27.             }
  28.             string str6 = "\t";
  29.             if (string.IsNullOrWhiteSpace(str6))
  30.             {
  31.                 Console.WriteLine("str6 is  empty"); ;
  32.             }
  33.             Console.ReadKey();
  34.         }
运行结果:
2 ]: ]& {9 O: V' ~  I 3.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-5-7 13:38

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表