本文介绍了只获取c#中的最后4位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个gridview控件...我想在这个网格视图中只显示信用卡的最后4位数...如何在c#中创建子串?
i have a gridview control...i wanna show only last 4 digit of credit card no in this grid view...how to make substring in c#??
推荐答案
string CCNo = "0000000000000000";
string XXNo = "XXXXXXXXXXXX";
string MaskedCCNo = String.Format("{0}{1}", XXNo, CCNo.Right(4));
你也可以简化这段代码。
You could simplify this code too.
string creditNumber = "1111222233334444"; ;
var last4 = creditNumber.Substring(creditNumber.Length - 4, 4);
参考: []
示例:
Reference:SubString[^]
Sample:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="credit" HeaderText="credit" />
<asp:BoundField DataField="last4" HeaderText="last4" />
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("credit", typeof(string));
dt.Columns.Add("last4", typeof(string));
dt.Rows.Add("11112222333344444","" );
dt.Rows.Add("11112222333344444" ,"");
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string creditNumber = DataBinder.Eval(e.Row.DataItem, "credit")+"";
var last4 = creditNumber.Substring(creditNumber.Length - 4, 4);
e.Row.Cells[1].Text = last4;
}
}
这篇关于只获取c#中的最后4位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!