How to separate special characters

So, I have the code, and I refined it so that it works as best as possible. Now it works fine, although I need to filter it throughout the sentence, regardless of any special characters wrapped around the word. For example, when I send a line:

JOIN GooGle | Γ— ,,. Β¬ hiring !HOteL, it is ++ !!free!! ,, ..!community;;+_

Prohibited words join, hiring, hotel, free, communitywill not define the sentence above.

My code is:

public bool CheckSentence(string messageText.ToLower())
{
    var count = 0;
    string[] wordsInMessage = messageText.Split(new char[] { ' ', ',' }, 
                                                StringSplitOptions.RemoveEmptyEntries);

    foreach (WordFilter Filter in this._filteredWords.ToList())
    {
        count += wordsInMessage.Count(x => x == Filter.Word);
    }

    return count >= 3;
}

If I remove special characters such as !from words, it will work. I could just add these characters to the char list, but certainly there is a very simple method?

+4
source share
3 answers

, . , , - .

, , , .

public bool CheckSentence(string messageText.ToLower())
{
    messageText = Regex.Replace(messageText, @"[^a-z0-9 ]", "");
    var count = 0;
    string[] wordsInMessage = messageText.Split(new char[] { ' ', ',' }, 
                                                StringSplitOptions.RemoveEmptyEntries);

    foreach (WordFilter Filter in this._filteredWords.ToList())
    {
        count += wordsInMessage.Count(x => x == Filter.Word);
    }

    return count >= 3;
}
+4
string testData = @"JOIN GooGle | Γ— ,,. Β¬ hiring !HOteL, it is ++!!free!! ,, ..!community; ; +_";
List<string> bannedWords = new List<string>
{
    "join", 
    "hiring", 
    "hotel", 
    "free", 
    "community"
};

bannedWords.ForEach(word =>
{
    int startIndex = testData.IndexOf(word, StringComparison.InvariantCultureIgnoreCase);
    if(startIndex == -1) return;
    testData = testData.Remove(startIndex, word.Length);
});
Console.WriteLine(testData);
0

, , -, . , . . , .

static String input = "JOIN GooGle | Γ— ,,. Β¬ hiring !HOteL, it is ++ !!free!! ,, ..!community;;+_";
static Regex charOnly = new Regex("[^a-zA-Z ]");
static Regex extarSpaces = new Regex(@"\s{2,}");
static List<String> bannedWords = new List<String> { "join", "hiring", "hotel", "free", "community" };

static void Main(string[] args) {
  string originalString = charOnly.Replace(input, "");
  originalString = extarSpaces.Replace(originalString, " ");
  originalString = originalString.ToLower();
  string[] splitArray = originalString.Split(' ');
  int count = 0;
  for (int i = 0; i < splitArray.Length; i++) {
    if (splitArray[i] != null) {
      if (bannedWords.Contains(splitArray[i].ToString())) {
        count++;
        Console.WriteLine("Banned: " + splitArray[i].ToString());
      }
    }
  }
  Console.WriteLine("originalString: " + originalString);
  Console.WriteLine("splitArray Size: " + splitArray.Length);
  Console.WriteLine("Banned Words in string = " + count);
  Console.ReadKey();
}
0

Source: https://habr.com/ru/post/1664976/


All Articles