1 /// <summary>
2 /// 过滤类
3 /// </summary>
4 class Filter
5 {
6 private string replaceChat = "*";
7 /// <summary>
8 /// 替代过滤后的字符
9 /// </summary>
10 public string ReplaceChat
11 {
12 get { return replaceChat; }
13 set { replaceChat = value; }
14 }
15
16 private string[] keys;
17 /// <summary>
18 /// 要过滤的关键字
19 /// </summary>
20 public string[] Keys
21 {
22 get { return keys; }
23 set
24 {
25 keys = value;
26 if (value != null && value.Length != 0)
27 {
28 foreach(string str in value)
29 {
30 string key = str.ToUpper();
31 Dictionary<char, object> dictionary = this.KeysDictionary;
32 foreach (char c in key)
33 {
34 if (!dictionary.ContainsKey(c))
35 {
36 dictionary.Add(c, new Dictionary<char, object>());
37 }
38 dictionary = dictionary[c] as Dictionary<char, object>;
39 }
40 dictionary.Add('\0', 0);
41 }
42 }
43 }
44 }
45 /// <summary>
46 /// 存放关键字的Dictionary字典
47 /// </summary>
48 private Dictionary<char, object> keysDictionary = new Dictionary<char, object>();
49
50 public Dictionary<char, object> KeysDictionary
51 {
52 get { return keysDictionary; }
53 set { keysDictionary = value; }
54 }
55
56 public Filter()
57 { }
58 /// <summary>
59 /// 构造器
60 /// </summary>
61 /// <param name="keys">过滤的关键字</param>
62 public Filter(string[] keys)
63 {
64 this.Keys = keys;
65 }
66 /// <summary>
67 /// 构造器
68 /// </summary>
69 /// <param name="keys">过滤的关键字</param>
70 /// <param name="replaceChat">代替过滤掉的字符串</param>
71 public Filter(string[] keys, string replaceChat)
72 {
73 this.Keys = keys;
74 this.ReplaceChat = replaceChat;
75 }
76
77 /// <summary>
78 /// 开始过滤工作
79 /// </summary>
80 /// <returns>过滤成功后的字符串</returns>
81 public string Replace(string content)
82 {
83 if (content == null || this.KeysDictionary.Count == 0)
84 {
85 return content;
86 }
87 else
88 {
89 int len = content.Length;
90 StringBuilder newContent = new StringBuilder(content.Length);//用来放新的结果
91 //开始每个字来过滤
92 for (int num = 0; num < len; num++)
93 {
94 Dictionary<char, object> dictionary = this.KeysDictionary;
95 int n = 0;//表达已经匹配到一个关键词里的几个字符了
96 bool isMatch = false;//是否匹配成功
97 char c = content[num];
98 //通过循环来检查是否匹配关键词
99 while (dictionary.ContainsKey(ToUpper(c)))
100 {
101 n++;
102 dictionary = dictionary[ToUpper(c)] as Dictionary<char, object>;
103 if (dictionary.ContainsKey('\0'))//说明完全匹配到一个关键词
104 {
105 for (int i = 0; i < n; i++)
106 {
107 newContent.Append(this.ReplaceChat);
108 }
109 num += n-1;
110 isMatch = true;
111 break;
112 }
113 else//说明还没有完全匹配到一个关键词
114 {
115 if (num + n < len)//说明还没完全匹配到一个关键词,且还可以向下再取一个
116 {
117 c = content[num + n];
118 }
119 else//说明已经不能再向下取一个了
120 {
121 break;
122 }
123 }
124 }
125 if (! isMatch)
126 {
127 newContent.Append(content[num]);
128 }
129 }
130 return newContent.ToString();
131 }
132 }
133
134 /// <summary>
135 /// 将字符转换成大写
136 /// </summary>
137 /// <param name="c">要转换的字符</param>
138 /// <returns>大写后的字符</returns>
139 private char ToUpper(char c)
140 {
141 if (c >= 'a' && c <= 'z')
142 {
143 return Convert.ToChar(c - 32);
144 }
145 return c;
146 }
147 }


编辑器加载中...

posted on 2012-01-25 00:51  风里的叶子  阅读(200)  评论(0)    收藏  举报