指示指定的字符串是 null 还是 Empty 字符串。其实就是判断字符串是空引用,或值为空。如刚定义一个字符串就是Empty(空)的。
命名空间: System
程序集: mscorlib(在 mscorlib.dll 中)
result = s == null || s == String.Empty;
语法:
public static bool IsNullOrEmpty(
string value
)
参数
- value
-
类型:System.String
要测试的字符串。
返回值
类型:System.Boolean
示例
稍微修改了下MSDN的例子。
protected void Page_Load(object sender, EventArgs e)
{
string s1 = "abcd";
string s2 = "";
string s3 = null;
string s4 = " ";
Response.Write(string.Format("String s1 {0}.", Test(s1)) + "<br />");
Response.Write(string.Format("String s2 {0}.", Test(s2)) + "<br />");
Response.Write(string.Format("String s3 {0}.", Test(s3)) + "<br />");
Response.Write(string.Format("String s4 {0}.", Test(s4)) + "<br />");
}
public static String Test(string s)
{
if (String.IsNullOrEmpty(s))
{
return "is null or empty";
}
else if(String.IsNullOrEmpty(s.Trim()))
{
return "its Trim is null or empty";
}
else
{
return String.Format("(\"{0}\") is not null or empty", s);
}
}
相关新闻
- 小程序登录流程图理解 2020-08-18
- 在C#中获取web.config中的配置信息 2021-08-23
- 小程序open-data头像样式 2021-04-10
- 小程序rich-text 富文本解析图片过大和图片路径的问题 2020-11-25
- C#中去掉字符串的最后一个字符 2020-11-23