C#中的字符串比较

来源:互联网 发布:担保行业 知乎 编辑:程序博客网 时间:2024/05/18 02:09

C#中的字符串比较

今天遇到了C#的字符串比较的问题,下面我们就来看看C#中的字符串怎么比较。

1>Str.Equals(“字符串”);
2>String.Compare(Str1, Str2) == 0
3> Str1.CompareTo(Str2) == 0
4>3. str1 == str2
String.Equals方法已经重载了,和==是一样的效果。

下面是我实际项目中碰到的:
/// <summary>
/// 点击按钮“机台查询”触发的方法。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
private void BtnCXJT_Click(object sender, EventArgs e) {
if(this.BtnCX.Text.Equals("机台查询")){
this.dgSbDetail.Visible = false;
this.BtnCX.Text = "查询明细";
this.BtnCX.BackColor = SystemColors.Info;
this.GetLySbDetail(this.str_sbid, this.str_sbname);
this.BtnCX.BackColor = Color.Lime;
this.txtBarcode.SelectAll();
this.txtBarcode.Focus();
}
else if (this.BtnCX.Text.Equals("隐藏明细"))
{
this.dgSbDetail.Visible = false;
this.txtBarcode.Enabled = true;
this.BtnCX.Text = "查询明细";
this.BtnCX.BackColor = Color.Lime;
this.txtBarcode.SelectAll();
this.txtBarcode.Focus();
}
else if (this.BtnCX.Text.Equals("查询明细"))
{
this.dgSbDetail.Visible = true;
this.GetLyDetail(this.str_sbid, GlobalPara.accountid);
this.BtnCX.Text = "隐藏明细";
this.BtnCX.BackColor = SystemColors.Info;
}
}

1 0