C # Sensitive keywords filter code

Source: Internet
Author: User

System.Text.StringBuilder sb = newSystem.Text.StringBuilder(text.Length);
            stringfilterText = "需要过滤的脏字 以|分开";//脏字 可根据自己的方式用分隔符
 
            string[] filterData = filterText.Split(‘|‘);
            foreach(var item infilterData)
            {
                charvalue = item[0];
                if(dicList.ContainsKey(value))
                    dicList[value].Add(item);
                else
                    dicList.Add(value, newList<string>() { item });
            }
 
            intcount = text.Length;
            for(inti = 0; i < count; i++)
            {
                charword = text[i];
                if(dicList.ContainsKey(word))//如果在字典表中存在这个key
                {
                    intnum = 0;//是否找到匹配的关键字  1找到0未找到
                    var data = dicList[word].OrderBy(g => g.Length);//把该key的字典集合按 字符数排序(方便下面从少往多截取字符串查找)
 
                    foreach(var wordbook indata)
                    {
                        if(i + wordbook.Length <= count)//如果需截取的字符串的索引小于总长度 则执行截取
                        {
                            stringresult = 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);
            }
            returnsb.ToString();
        }
        /// <summary>
        /// 替换星号
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        privatestatic string GetString(stringvalue)
        {
            stringstarNum = string.Empty;
            for(inti = 0; i < value.Length; i++)
            {
                starNum += "*";
            }
            returnstarNum;
        }
//该代码片段来自于: http://www.sharejs.com/codes/csharp/5486

C # Sensitive keywords filter code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.