一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net C# 关键字过滤程序代码

时间:2022-06-25 08:20:19 编辑:袖梨 来源:一聚教程网

 代码如下 复制代码

例1

  ///


        /// 过滤关键字
        ///

        /// 过滤字眼,用| 隔开
        /// 传入要过滤的数据
        ///
        public List FriterText(string value, string text)
        {
            List FriterList = new List();
            String regex = string.Format("({0})", value);
            //String regex = "(插你|企业|企业家)";
            Regex rg = new Regex(regex);
            if (rg.IsMatch(text))
            {
                MatchCollection mc = rg.Matches(text);
                foreach (var item in mc)
                {
                    if (!FriterList.Contains(item.ToString()))
                        FriterList.Add(item.ToString());
                }
            }
            return FriterList;
        }

例1


class Program
{
static void Main(string[] args)
{
string keys = "云风博客|并发问题|最烂的方法|内存|集合|数据库|性能";

using (new OperationTimer("keyWords:"))
{
WordSearch ws = new WordSearch(keys);
string str = "看云风博客关于解决12306并发问题的启发:我现在做骏卡接口,可能出现并发问题,就是一个订单可能向我们的接口发送多个请求,而我现在做的方法是去数据库中对应的表验证,看订单是否存在,如果存在就提示一下,如果不存在按流程走,但是这个样每来一个订单我都需要去数据库查,如果我在内存中维护一个订单集合,这样就能很快解决判断订单是否存在的问题,惯性思维太严重了,什么都去数据库查,这样的性能是最差的,其实很多问题在内存中就可以搞定的,最近还有一个特别感受,不要做井底之蛙,多看牛人的东西收获真的比自己埋头写代码进步快很多,其实很多时候我写的程序性能差,效率低都是因为方法的原因,没有找到好的方法,没有灵光一闪的感觉,用了最烂的方法解决问题";
ConsoleColor color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("原文:");
Console.ForegroundColor = color;
Console.WriteLine(str);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("过滤后:");
Console.ForegroundColor = color;
Console.WriteLine(ws.Filter(str));
}
Console.Read();
}
}


例3

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;

namespace BLL.Common
{
    #region 操作类
    public class KeywordsFilter
    {
       

        #region 关键字过滤
        ///


        /// 关键字过滤
        ///
        ///

        ///
        ///
        public static string Filter(string keywords)
        {
 
            //需过滤关键字集合
            List badwords = new List();
           
            KeywordsFilterClass kf = new KeywordsFilterClass();
            keywords = kf.BadwordInKeywords(keywords, badwords);
            return keywords;
        }
        #endregion

    }
    #endregion

    #region 关键字过滤类
    ///


    /// 关键字过滤类
    ///

    public class KeywordsFilterClass
    {

        private Dictionary hash = new Dictionary();
        //脏字字典 开头脏字存储
        private BitArray firstCharCheck = new BitArray(char.MaxValue);
        //脏字字典 单个char存储
        private BitArray allCharCheck = new BitArray(char.MaxValue);
        private int maxLength = 0;

        ///


        /// 初始化 已存储的 过滤字符串
        ///

        ///
        private void InitHash(List badwords)
        {
            foreach (string word in badwords)
            {
                //保存字典内不存在的脏字
                if (!hash.ContainsKey(word))
                {
                    hash.Add(word, null);
                    //设置脏字计算长度
                    this.maxLength = Math.Max(this.maxLength, word.Length);
                    firstCharCheck[word[0]] = true;
                    foreach (char c in word)
                    {
                        allCharCheck[c] = true;
                    }
                }
            }

        }
        ///


        /// 替换字符串中的脏字为指定的字符
        ///

        ///
        ///
        public string BadwordInKeywords(string text, List badwords)
        {
            //初始化 脏字字典
            this.InitHash(badwords);
            int index = 0;

            while (index < text.Length)
            {
                //判断开头脏字
                if (!firstCharCheck[text[index]])
                {
                    //未找到开头脏字 则索引累加
                    while (index < text.Length - 1 && !firstCharCheck[text[++index]]) ;
                }
                for (int j = 1; j <= Math.Min(maxLength, text.Length - index); j++)
                {
                    if (!allCharCheck[text[index + j - 1]])
                    {
                        break;
                    }
                    string sub = text.Substring(index, j);

                    if (hash.ContainsKey(sub))
                    {
                        text = text.Replace(sub, "**");
                        //this.InitHash(badwords);
                        index += j;
                        break;
                    }
                }

                index++;
            }
            return text;
        }


    }
    #endregion
}

热门栏目