初始化这些数据的方法如下: 复制代码 代码如下: foreach (string word in badwords) { if (!hash.ContainsKey(word)) { hash.Add(word, null); maxlength = Math.Max(maxlength, word.Length); firstCharCheck[word[0]] = true;
foreach (char c in word) { allCharCheck[c] = true; } } }
判断脏字是否出现在一个字符串中的代码如下:
复制代码 代码如下: int index = 0; int offset = 0; while (index < text.Length) { if (!firstCharCheck[text[index]]) { while (index < text.Length - 1 && !firstCharCheck[text[++index]]) ; }