zoukankan      html  css  js  c++  java
  • C#敏感关键词过滤代码

    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<string>() { item });
                }
      
                int count = text.Length;
                for (int i = 0; i < count; i++)
                {
                    char word = text[i];
                    if (dicList.ContainsKey(word))//如果在字典表中存在这个key
                    {
                        int num = 0;//是否找到匹配的关键字  1找到0未找到
                        var data = dicList[word].OrderBy(g => g.Length);//把该key的字典集合按 字符数排序(方便下面从少往多截取字符串查找)
      
                        foreach (var wordbook in data)
                        {
                            if (i + wordbook.Length <= count)//如果需截取的字符串的索引小于总长度 则执行截取
                            {
                                string result = text.Substring(i, wordbook.Length);//根据关键字长度往后截取相同的字符数进行比较
                                if (result == wordbook)
                                {
                                    num = 1;
                                    sb.Append(GetString(result));
                                    i = i + wordbook.Length - 1;//比较成功 同时改变i的索引
                                    break;
                                }
                            }
                        }
                        if (num == 0)
                            sb.Append(word);
                    }
                    else
                        sb.Append(word);
                }
                return sb.ToString();
            }
            /// <summary>
            /// 替换星号
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            private static string GetString(string value)
            {
                string starNum = string.Empty;
                for (int i = 0; i < value.Length; i++)
                {
                    starNum += "*";
                }
                return starNum;
            }
    //该代码片段来自于: http://www.sharejs.com/codes/csharp/5486
  • 相关阅读:
    [转]PublishingFeatureHandler(发布功能参数)
    抽象类与接口使用的MSDN建议
    [转]SharePoint内容定制之XSLT高级用法——使用Muenchian方法分组XML数据
    MOSS中的DelegateControl机制
    [转]SharePoint内容定制之XSLT高级用法——带返回值的函数调用
    问题:加了访问权限也无法访问网站。
    [转]Writing Custom Entries to the Audit Log in Windows SharePoint Services 3.0(如何记录自定义审核日志)
    .NET使用C#连接TIBCO的JMS消息服务
    [转]SharePoint 2007 and 2010 Farm ports – configuring firewall(如何配置服务器场中的防火墙)
    [转] SharePoint Features elements, scope and other info
  • 原文地址:https://www.cnblogs.com/wwy224y/p/4730591.html
Copyright © 2011-2022 走看看