private string Method1(string text)
{
string t = text;
// t = Regex.Replace(t, @"(.)\1+", "$1");
while (Regex.IsMatch(t, @"(.+|.)([^\1]*?)(\1)"))//(.)([^\1]+?)(\1)
{
t = Regex.Replace(t, @"(.+|.)([^\1]*?)(\1)", "$1$2");
}
return t;
}
private string Method2(string text)
{
string t =text;
//t = Regex.Replace(t, @"(.)\1+", "$1");
int i = 0;
while (Regex.IsMatch(t, @"(.)([^\1]*?)(\1)"))
{
i++;
t = Regex.Replace(t, @"(.)([^\1]*?)(\1)", "$1$2");
}
// atb2.Text = Regex.Replace(t, @"(.)\1+", "$1");
Response.Write(i);return t;
}
本文探讨了使用正则表达式去除字符串中重复字符的方法,提供了两种不同的实现方式,并邀请读者分享更优的解决方案。
2185

被折叠的 条评论
为什么被折叠?



