C#中的isnullorempty怎么使用

   2025-02-07 7760
核心提示:在C#中,IsNullOrEmpty是一个静态方法,用于判断一个字符串是否为null或者空字符串。使用时,直接通过字符串类型调用该方法即可

在C#中,IsNullOrEmpty是一个静态方法,用于判断一个字符串是否为null或者空字符串。使用时,直接通过字符串类型调用该方法即可。以下是使用示例:

string str1 = null;string str2 = "";string str3 = "Hello World";if (string.IsNullOrEmpty(str1)){    Console.WriteLine("str1 is null or empty");}else{    Console.WriteLine("str1 is not null or empty");}if (string.IsNullOrEmpty(str2)){    Console.WriteLine("str2 is null or empty");}else{    Console.WriteLine("str2 is not null or empty");}if (string.IsNullOrEmpty(str3)){    Console.WriteLine("str3 is null or empty");}else{    Console.WriteLine("str3 is not null or empty");}

输出结果为:

str1 is null or emptystr2 is null or emptystr3 is not null or empty

以上示例中,IsNullOrEmpty方法分别判断了str1str2str3是否为null或空字符串,并根据判断结果输出相应的信息。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言