A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 万大述 中级黑马   /  2013-6-21 17:38  /  1170 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本想把输入和事先定义的数组比较,如果有,则替换成“*”。可。。。事不尽如人意。求指点,代码如下:
string inPut = Console.ReadLine();
            char[] inPutExchange = inPut.ToArray(); //转换为字符数组。
            string [] test={"暴力","血腥","TMD"}; //比较数组。
            for (int i = 0; i < test.Length; i++) //比较并替换。
            {
                if (Equals(inPutExchange, test[i]))
                {
                    inPut = "*";
                    Console.WriteLine(inPut);
                }
                else
                {
                    Console.WriteLine(inPut);
                    break;
                }
             }
            Console.ReadKey();

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

3 个回复

倒序浏览
string inPut = Console.ReadLine();
string inPut1 = regex.replace(input,"(暴力)|(血腥) |(TMD) ","*");
            Console.WriteLine(inPut1);
            Console.ReadKey();

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
字符串和字符数组比较么
回复 使用道具 举报
我只能做到一个敏感词的替换,多个还是不会写。希望楼下能有更好的方法      
      string str = Console.ReadLine();
            string a = "暴力";
            Regex r = new Regex(a);
            Match m = r.Match(str);
            if (m.Success)
            {
                str = str.Replace(a, "***");
                Console.Write(str);

            }
            else
            {
                Console.Write(str);
            }
            Console.ReadKey();

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马