概要
经常会用到,判断字符是非为“”和null的情况。判断到不难,但是每次都很麻烦。看看有没有封装的更简单的函数,因为这个场景会经常用,一看真有。
代码
namespace 判断字符串是否为空2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("判断字符串是否为空");
string atr = null;
if (string.IsNullOrEmpty(atr)) {
Console.WriteLine("字符串为null");
}
string str2 = "";
if (string.IsNullOrEmpty(str2)) {
Console.WriteLine("字符串为\"\"");
}
Console.ReadKey();
}
}
}
运行结果

这篇博客介绍了如何在C#中使用`string.IsNullOrEmpty()`方法来快速判断字符串是否为空或null,从而避免重复的条件判断。通过示例代码展示了该函数的使用方式,简化了常见的字符串空值检查操作。
3802

被折叠的 条评论
为什么被折叠?



