开发者

how to replace special character from string in asp.net

开发者 https://www.devze.com 2023-01-16 22:07 出处:网络
my code - txtPhoneWork.Text.Replace(\"-\",\"\"); txtPhoneWork.Text.Replace(\"_\", \"\"); txtMobile.Text.Replace(\"-\", \"\");

my code -

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

        loca开发者_运维百科tion.ContactWork = txtPhoneWork.Text.Trim();
        location.ContactMobile = txtMobile.Text.Trim();
        location.ContactOther = txtPhoneOther.Text.Trim();

but it is not replacing and is there any method so that both - and _ can be replaced in single function.


.Replace() returns the string with the replacement performed (it doesn't change the original string, they're immutable), so you need a format like this:

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


get the replaced string in some variable

you can try this to replace multiple characters in single function string value= System.Text.RegularExpressions.Regex.replace(value, @"[-_]", "");

0

精彩评论

暂无评论...
验证码 换一张
取 消