去除重复字符串,效率
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;
}
希望大家帮忙看看,有更好的方法请写出来
{
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;
}