温馨提示×

如何判断C#中的字符串是否为空

c#
小樊
81
2024-08-23 18:57:26
栏目: 编程语言

在C#中,可以使用以下方法来判断字符串是否为空:

  1. 使用 String.IsNullOrEmpty 方法:
string str = "test";
if (String.IsNullOrEmpty(str))
{
    Console.WriteLine("String is null or empty.");
}
else
{
    Console.WriteLine("String is not null or empty.");
}
  1. 使用 String.IsNullOrWhiteSpace 方法:
string str = "test";
if (String.IsNullOrWhiteSpace(str))
{
    Console.WriteLine("String is null, empty, or consists only of white-space characters.");
}
else
{
    Console.WriteLine("String is not null, empty, or consists only of white-space characters.");
}

这两种方法可以帮助您确定字符串是否为空或仅包含空格或制表符等空白字符。

0