温馨提示×

c# string怎样判断是否为空

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

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

string str = "your string here";

if (string.IsNullOrEmpty(str))
{
    Console.WriteLine("The string is empty or null.");
}
else
{
    Console.WriteLine("The string is not empty.");
}

string.IsNullOrEmpty(str)方法会检查字符串str是否为null或者为空字符串(即长度为0)。如果满足这些条件之一,该方法将返回true,否则返回false。

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

推荐阅读:c# nullable 怎样判断是否为空

0