本文实例讲述了C#敏感词过滤实现方法。分享给大家供大家参考。具体如下:
这两天突然想到了敏感词过滤 就结合网上找到的资料自己写了一个,脏字数量700+(效率不是很高 测试在110多KB的情况下比replace快 3-4倍)
测试结果图 单位:秒
代码如下:
System.Text.StringBuilder sb = new System.Text.StringBuilder(text.Length);
string filterText = "需要过滤的脏字 以|分开";
//脏字 可根据自己的方式用分隔符
string[] filterData = filterText.Split('|');
foreach (var item in filterData)
{
char value = item[0];
if (dicList.ContainsKey(value))
dicList[value].Add(item);
else
dicList.Add(value, new List() { item });
}
int count = text.Length;
for (int i = 0; i < count; i++)
{
char word = tex