大众信息网

C# 删除字符串中特定的字符

关注:147  答案:6  手机版
解决时间 2021-02-25 12:43
删除字符串中特定的字符。。比如 XX省XX市XX区XX街道,,把省 市 区 街道删掉 。。。让字符串变为XXXXXXXX。。有没有比较简单的方法可以实现的 。。
最佳答案
前台: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

后台 protected void Button1_Click(object sender, EventArgs e)
{
string ss = this.TextBox1.Text.ToString();
string newSS= ss.Replace("省", "").Replace("市", "").Replace("区", "").Replace("街", "");
this.TextBox1.Text = newSS;

}
我的测试结果:一开始在文本框中输入:黑龙江省哈尔滨市南岗区红旗大街

接着按button按钮就会 进入后台 返回的结果是: 黑龙江哈尔滨南岗红旗大街

希望能帮到你
全部回答
string fullPath="123456"; string ext="34"; string result = System.Text.RegularExpressions.Regex.Replace(fullPath, ext, "");  Console.WriteLine(result );//"1256"望采纳 谢谢采纳!!!
string a = " XX省XX市XX区XX街道"; string b = a.tostring().replace("省","").replace(" 市","").replace("区","").replace(" 街道",""); 这样就搞定了;
string temp1="---广东广州市"; string temp2="----广东广州市海珠区"; if (temp1.substring(0,3)=="---") temp1.replace("---",""); if (temp2.substring(0,4)=="----") temp2.replace("----","");
string result = System.Text.RegularExpressions.Regex.Replace(特定字符串, "[省市区街道]", ""); 一句话搞定。 谢谢采纳
可以使用Replace() 方法把你需要删除的值 替换为‘’ 比如 string sAddress = " XX省XX市XX区XX街道"; sAddress.Replace("省", "").Replace("市", "").Replace("街道", "");
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!