温馨提示×

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

c#
小樊
116
2024-08-23 18:57:26
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在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.");
}

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

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:c语言怎么判断字符串是否为空

0