2017-08-03 60 views
0

我正在嘗試創建一個單詞過濾器,以阻止在聊天中廣播的文本文件中列出的任何單詞。在聊天中阻止字符串大寫/小寫C#

最初的代碼非常有效,但只有聊天中說的單詞與文本文件中的單詞相同時才起作用。

我需要一種方法來阻止這個單詞,無論它是全部大寫,全部是小寫還是中間的任何內容,但它必須完全匹配列表中的單詞。

例如,它不會過濾「ship,SHIP或ShIp」,但它會過濾..「Sh **,SH **」,您會明白。我接受其他方法,不包括製作一個巨大的文本文件列表。

 foreach (var word in File.ReadAllLines("wordlist.txt")) 
      { 
       if (message.Contains(word)) 
       player.SendToSelf(Channel.Reliable, ClPacket.GameMessage, "You Cannot Say That Here!"); 


      } 

      return true; 
} 

我已經試過

if (char.IsUpper(message[0])) 
      player.SendToSelf(Channel.Reliable, ClPacket.GameMessage, "You Cannot Say That Here!"); 

喜歡的東西的CurrentCulture/InvariantCultureIgnoreCase/OrdinalIgnoreCase可以使用,但我不知道如何實現這樣的事情。

對於使用外來字符阻止外國(En除外)語言中的單詞也非常有益。

+0

這可能是有趣:https://開頭stackoverflow.com/a/15464440/4499267 – Phate01

+0

@ Phate01謝謝!我不確定如何實現這一點,但是..正如我所說我很新這一點。 – Cinn

回答

2

從這個MSDN文章string.Compare

public static class StringExtensions 
{ 
    public static bool Contains(this String str, String substring, 
           StringComparison comp) 
    {        
     if (substring == null) 
     throw new ArgumentNullException("substring", 
             "substring cannot be null."); 
     else if (! Enum.IsDefined(typeof(StringComparison), comp)) 
     throw new ArgumentException("comp is not a member of StringComparison", 
            "comp"); 

     return str.IndexOf(substring, comp) >= 0;      
    } 
} 

您可以將此靜態類添加到您的來源和使用StringComparison.InvariantCulture作爲論證中StringComparison

if (message.Contains(word, StringComparison.InvariantCulture)) 
    //blah