(转)String.IsNullOrEmpty()方法的使用

String.IsNullOrEmpty()方法的使用

dgdisme 2014-08-05 14:10:19 5329 收藏 2
分类专栏: .net
版权
String.IsNullOrEmpty()方法是在 .NET Framework 2.0 版中是新增的。

指示指定的 String 对象是 空引用(在 Visual Basic 中为 Nothing) 还是空字符串。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StringIsNull
{
public partial class _Default : System.Web.UI.Page
{
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);
        }
    }
}

}
得出的结果是 :

String s1 ("abcd") is not null or empty.
String s2 is null or empty.
String s3 is null or empty.
String s4 its Trim is null or empty.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值