如何在asp.net中替换字符串中的特殊字符

如何在asp.net中替换字符串中的特殊字符,asp.net,replace,Asp.net,Replace,我的代码- txtPhoneWork.Text.Replace("-",""); txtPhoneWork.Text.Replace("_", ""); txtMobile.Text.Replace("-", ""); txtMobile.Text.Replace("_", ""); txtPhoneOther.Text.Replace("-", ""); txtPhoneOther.Text.Replace("_"

我的代码-

txtPhoneWork.Text.Replace("-","");
        txtPhoneWork.Text.Replace("_", "");
        txtMobile.Text.Replace("-", "");
        txtMobile.Text.Replace("_", "");
        txtPhoneOther.Text.Replace("-", "");
        txtPhoneOther.Text.Replace("_", "");

        location.ContactWork = txtPhoneWork.Text.Trim();
        location.ContactMobile = txtMobile.Text.Trim();
        location.ContactOther = txtPhoneOther.Text.Trim();
但它不是替换,是否有任何方法可以在单个函数中替换
-
-

返回执行替换的字符串(它不会更改原始字符串,它们是不可变的),因此您需要这样的格式:

txtPhoneWork.Text = txtPhoneWork.Text.Replace("-","");

在某个变量中获取替换的字符串

您可以尝试在单个函数中替换多个字符
字符串值=System.Text.RegularExpressions.Regex.replace(值@“[-\]”,“”)

在“”中有一个空格,替换(“-”,“”);或替换(“-”,”);由于字符不同,您应该为代码定义不同的函数。如果相同的字符替换意味着您可以使用单个函数