我有一个正则表达式来查找nth字符串中出现的字符,这里是代码:
public static int NthIndexOf(this string target, string value, int n)
{
Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}");
if (m.Success)
{
return m.Groups[2].Captures[n - 1].Index;
}
else
{
return -1;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我在这个字符串中有1594个条目,有1593个分号.如果我写:
tempstring.NthIndexOf(";", 1593)
Run Code Online (Sandbox Code Playgroud)
答案立即正确回复.如果我给它任何超过1594的东西它会挂起.有谁知道如何解决这一问题?
string holder = "test;test2;test3";
string test = "";
for (int i = 0; i < 600; i++)
{
test += holder;
}
int index = test.NthIndexOf(";", 2000);
Run Code Online (Sandbox Code Playgroud)
这需要很长时间.将600更改为6,速度非常快.使2000至1700,它也非常快.
为什么我的正则表达式如此之慢?
如果你真的只是寻找字符重复,而不是字符串重复,那么你应该能够用简单的东西替换你的方法
public static int NthIndexOf(this string target, char testChar, int n)
{
int count = 0;
for(int i=0; i<target.Length; i++)
{
if(target[i] == testChar)
{
count++;
if(count == n) return i;
}
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
并使用它.它的限制应该少得多.
至于为什么你的原始正则表达式变慢,这是我怀疑的:
| 归档时间: |
|
| 查看次数: |
1447 次 |
| 最近记录: |