/// <summary>
        /// illegal word filter
        /// </summary>
        /// <param name="str">input string</param>
        /// <param name="illegalwords">illegal words spilt by '|'</param>
        /// <returns>legal words</returns>
        public static string Filter(string str, string illegalwords)
        {
            var ill = new IllegalWordsSearchEx();

            ill.SetKeywords(illegalwords.Split('|'));
            return(ill.Replace(str, '*'));
        }
Beispiel #2
0
        public void IllegalWordsSearchExTest()
        {
            string s    = "中国|国人|zg人|f**k|all|as|19|http://|ToolGood";
            string test = "我是中国人";


            IllegalWordsSearchEx iwords = new IllegalWordsSearchEx();

            iwords.SetKeywords(s.Split('|'));


            var b = iwords.ContainsAny(test);

            Assert.AreEqual(true, b);


            var f = iwords.FindFirst(test);

            Assert.AreEqual(true, f.Success);
            Assert.AreEqual("中国", f.Keyword);
            Assert.AreEqual(2, f.Start);
            Assert.AreEqual(3, f.End);



            var all = iwords.FindAll(test);

            Assert.AreEqual("中国", all[0].SrcString);
            Assert.AreEqual("国人", all[1].SrcString);

            test = "我是中国zg人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("中国", all[0].SrcString);
            Assert.AreEqual("zg人", all[1].SrcString);

            test = "中间国zg人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("zg人", all[0].SrcString);

            test = "f**k al[]l";
            all  = iwords.FindAll(test);
            Assert.AreEqual("f**k", all[0].SrcString);
            Assert.AreEqual("al[]l", all[1].SrcString);
            Assert.AreEqual(2, all.Count);

            test = "http://ToolGood.com";
            all  = iwords.FindAll(test);
            Assert.AreEqual("ToolGood", all[0].Keyword);
            Assert.AreEqual("ToolGood", all[0].SrcString);
            Assert.AreEqual(1, all.Count);


            test = "asssert all";
            all  = iwords.FindAll(test);
            Assert.AreEqual("as", all[0].SrcString);
            Assert.AreEqual("ass", all[1].SrcString);
            Assert.AreEqual("asss", all[2].SrcString);
            Assert.AreEqual("all", all[3].SrcString);
            Assert.AreEqual(4, all.Count);



            test = "我是【中]国【人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("中]国", all[0].SrcString);
            Assert.AreEqual("国【人", all[1].SrcString);

            test = "我是【中国【人";
            all  = iwords.FindAll(test);
            Assert.AreEqual("中国", all[0].SrcString);
            Assert.AreEqual("国【人", all[1].SrcString);
            Assert.AreEqual(2, all.Count);


            var ss = iwords.Replace(test, '*');

            Assert.AreEqual("我是【****", ss);
        }