时间:2021-07-01 10:21:17 帮助过:8人阅读
private static Dictionary dic = new Dictionary();
private static BitArray fastcheck = new BitArray(char.MaxValue);
static void Prepare()
{
string[] badwords = // read from file
foreach (string word in badwords)
{
if (!dic.ContainsKey(word))
{
dic.Add(word, null);
maxlength = Math.Max(maxlength, word.Length);
fastcheck[word[0]] = true;
}
}
}
使用的时候
代码如下:
int index = 0;
while (index < target.Length)
{
if (!fastcheck[target[index]])
{
while (index < target.Length - 1 && !fastcheck[target[++index]]) ;
}
for (int j = 0; j < Math.Min(maxlength, target.Length - index); j++)
{
string sub = target.Substring(index, j);
if (dic.ContainsKey(sub))
{
sb.Replace(sub, "***", index, j);
index += j;
break;
}
}
index++;
}