Linux 拨号vps windows公众号手机端

C#中的isnullorempty怎么使用

lewis 8年前 (2017-04-23) 阅读数 6 #程序编程
文章标签 c#

在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 empty
str2 is null or empty
str3 is not null or empty

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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门