0

what is the equivalent of below:

contact.name.Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "");

Thanks

1

3 Answers 3

2

Regular Expression Replace should do the trick

contact.name = Regex.Replace(contact.name, @"[\(\)\- ]", String.Empty);
Sign up to request clarification or add additional context in comments.

Comments

1

Using LINQ:

var s = "abcd 238(23)2342-23";
var exclusion = "()- ";

var result = new string(s.ToCharArray().Where (x => !exclusion.Contains(x)).ToArray());

or

var s = "abcd 238(23)2342-23";

var result = new string(s.ToCharArray().Where (x => !"()- ".Contains(x)).ToArray());

Comments

0

StringBuilder will be more effecient if you're doing more than a few replacements.

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.