什么是下面的等价:如何从字符串中替换多个字符?
contact.name.Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "");
感谢
什么是下面的等价:如何从字符串中替换多个字符?
contact.name.Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", "");
感谢
正则表达式Replace应该做的伎俩
contact.name = Regex.Replace(contact.name, @"[\(\)\- ]", String.Empty);
StringBuilder的将是更多的effecient如果你超过几替代做得更多。
使用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());
检查: http://stackoverflow.com/questions/6657278/whats-the-best-way-to-remove -strings –