注意:此方法在 .NET Framework 2.0 版中是新增的。
IsNullOrEmpty 是一种简便方法,它使您能够同时测试 String 是否为 空引用(在 Visual Basic 中为 Nothing) 或其值是否为 Empty。
// This example demonstrates the String.IsNullOrEmpty() method
using System;
class Sample
{
public static void Main()
{
string s1 = "abcd";
string s2 = "";
string s3 = null;
Console.WriteLine("String s1 {0}.", Test(s1));
Console.WriteLine("String s2 {0}.", Test(s2));
Console.WriteLine("String s3 {0}.", Test(s3));
}
public static String Test(string s)
{
if (String.IsNullOrEmpty(s) == true)
return "is null or empty";
else
return String.Format("(/"{0}/") is not null or empty", s);
}
}
/*
This example produces the following results:
String s1 ("abcd") is not null or empty.
String s2 is null or empty.
String s3 is null or empty.
*/
本文介绍了一个简单的方法来检查 .NET Framework 2.0 中的字符串是否为空或为 null。通过使用 String.IsNullOrEmpty 方法,可以轻松判断字符串变量是否为 null 或其长度是否为零。
1017

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



