2011-07-16 80 views
0

什么是下面的等价:如何从字符串中替换多个字符?

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

感谢

+0

检查: http://stackoverflow.com/questions/6657278/whats-the-best-way-to-remove -strings –

回答

2

正则表达式Replace应该做的伎俩

contact.name = Regex.Replace(contact.name, @"[\(\)\- ]", String.Empty); 
0

StringBuilder的将是更多的effecient如果你超过几替代做得更多。

1

使用LINQ:

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

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

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

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