想要过滤用户输入的一些不合法的发言,比如 TMD、SB什么的。但肯定有这样的T☆M★D这样的,没有什么好办法,大家有什么好办法吗?最笨的办法倒是有,但工作量太大,就是把所有特殊字符做成一个字符库。

解决方案 »

  1.   

    这里有http://www.cnblogs.com/xingd/archive/2008/01/31/1060425.html.NET脏字过滤算法 你看看
      

  2.   

    用regex來處理吧﹐它是專門做這種事情的,如以下就是只能輸入1~5數字的檢查
    Regex regexCol = new Regex("^([1-5])$");
    bool flagcol = false;
    while (!flagcol)
    {
        Console.Write("Number of Columns:");
        entercol = Console.ReadLine();
        if (regexCol.IsMatch(entercol))
           {
            col = Convert.ToInt32(entercol);
            flagcol = true;
           }
        else
           {
             Console.WriteLine("Cannot create spreadsheet:the maximum number of columns are 5 respectively");
           }
    }
      

  3.   

    public string Replace (string oldValue,string newValue)