2

my code -

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();

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

1
  • As characters are different you should define seperate functions to your code. If same character replacement means you can have a single function Commented Sep 16, 2010 at 11:49

2 Answers 2

15

.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("-","");
Sign up to request clarification or add additional context in comments.

1 Comment

there would be a space in "", Replace("-"," "); OR Replace("-","");
0

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, @"[-_]", "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.