Exemple #1
0
 /// <summary>
 /// 替换非法字符
 /// </summary>
 /// <param name="text"></param>
 /// <param name="mask">用于代替非法字符</param>
 /// <returns>替换后的字符串</returns>
 public string Replace(string text, char mask = '*')
 {
     char[] chars = null;
     for (int head = 0; head < text.Length; head++)
     {
         int      index = head;
         TrieNode node  = this;
         while (node.TryGetValue(GetSimp(text[index]), out node))
         {
             if (node.m_end)
             {
                 if (chars == null)
                 {
                     chars = text.ToArray();
                 }
                 for (int i = head; i <= index; i++)
                 {
                     chars[i] = mask;
                 }
                 head = index;
             }
             if (text.Length == ++index)
             {
                 break;
             }
         }
     }
     return(chars == null ? text : new string(chars));
 }
Exemple #2
0
 /// <summary>
 /// 查找所有非法字符
 /// </summary>
 /// <param name="text"></param>
 /// <returns></returns>
 public IEnumerable <string> FindAll(string text)
 {
     for (int head = 0; head < text.Length; head++)
     {
         int      index = head;
         TrieNode node  = this;
         while (node.TryGetValue(GetSimp(text[index]), out node))
         {
             if (node.m_end)
             {
                 yield return(text.Substring(head, index - head + 1));
             }
             if (text.Length == ++index)
             {
                 break;
             }
         }
     }
 }
Exemple #3
0
 /// <summary>
 /// 检查是否包含非法字符
 /// </summary>
 /// <param name="text">输入文本</param>
 /// <returns>找到的第1个非法字符.没有则返回string.Empty</returns>
 public bool HasBadWord(string text)
 {
     for (int head = 0; head < text.Length; head++)
     {
         int      index = head;
         TrieNode node  = this;
         while (node.TryGetValue(GetSimp(text[index]), out node))
         {
             if (node.m_end)
             {
                 return(true);
             }
             if (text.Length == ++index)
             {
                 break;
             }
         }
     }
     return(false);
 }
Exemple #4
0
 /// <summary>
 /// 检查是否包含非法字符
 /// </summary>
 /// <param name="text">输入文本</param>
 /// <returns>找到的第1个非法字符.没有则返回string.Empty</returns>
 public string FindOne(string text)
 {
     for (int head = 0; head < text.Length; head++)
     {
         int      index = head;
         TrieNode node  = this;
         while (node.TryGetValue(GetSimp(text[index]), out node))
         {
             if (node.m_end)
             {
                 return(text.Substring(head, index - head + 1));
             }
             if (text.Length == ++index)
             {
                 break;
             }
         }
     }
     return(string.Empty);
 }
Exemple #5
0
        /// <summary>
        /// 查找所有非法字符
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List <string> FindAll(string text)
        {
            List <string> result = new List <string>();

            for (int head = 0; head < text.Length; head++)
            {
                int      index = head;
                TrieNode node  = this;
                while (node.TryGetValue(text[index], out node))
                {
                    if (node.m_end)
                    {
                        result.Add(text.Substring(head, index - head + 1));
                    }
                    if (text.Length == ++index)
                    {
                        break;
                    }
                }
            }
            return(result);
        }