温馨提示×

C#中的isnullorempty怎么使用

c#
小亿
95
2024-01-09 17:50:56
栏目: 编程语言

在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或空字符串,并根据判断结果输出相应的信息。

0