如何检查String是否包含单词而不是实际的String

时间:2022-09-08 01:44:20

The title might be a little confusing, here's what's happening. I've got this piece of code:

标题可能有点令人困惑,这就是正在发生的事情。我有这段代码:

private List<String> aswear = Arrays.asList("anus", "arse", "arsehole", "ass", "ass-hat", "ass-jabber", "ass-pirate", "assbag"); // A list with all swears starting with a.

    String message = event.getMessage(); // When a player sends a message to the server, this is his message.

    boolean asSwear = false; // A boolean to check if his sentence has a double swear.

    for(String as : aswear) { // Loop through all the swears.

        if (asSwear == false) { // Check if there is already an a swear.

            if (message.contains(as)) { // If his message contains a swear

                event.setCancelled(true); // Remove the message from the server

                event.getBukkitPlayer().sendMessage(PredefinedMessages.PHOENIX_SWEARING_DETECTED.build()); // Send a message saying he sweared.

                asSwear = true; // Set the boolean to true because he sweared.
            }

        }
    }

My problem is that when a player writes "Bass" in his message it'll block, yet bass isn't a swear. It blocks because it contains 'ass' in the message. Does anybody have a fix to this issue?

我的问题是,当一个玩家在他的消息中写下“低音”时它会阻止,但低音并不是一个发誓。它会阻止,因为它在消息中包含“ass”。有人解决了这个问题吗?

7 个解决方案

#1


0  

Use message.split("\\b") to get an array of all "words" in the message. For each word, check if your blacklist contains that word:

使用message.split(“\\ b”)获取消息中所有“单词”的数组。对于每个单词,检查您的黑名单是否包含该单词:

String[] msgWords = message.split("\\b");
for (String word : msgWords) {
  if (aswear.contains(word.toLowerCase(Locale.US))) {
    // do whatever has to be done
    break;
  }
}

EDIT: Used \\b as word boundary regex, which really simplifies things (slightly stolen from Kent's answer).

编辑:使用\\ b作为单词边界正则表达式,这真的简化了事情(肯特的答案略有被盗)。

#2


3  

use regex check instead of String.contains. Then you can match word-boundary using \b

使用正则表达式检查而不是String.contains。然后你可以使用\ b匹配字边界

#3


0  

How about to define the array as " ass " with spaces around the words and use the same logic?

如何将数组定义为“ass”,并在单词周围添加空格并使用相同的逻辑?

#4


0  

How about a reversing the logic to iterate over player message with space as splitter and then checking whether that word is in your swear collection? That will give control

如何反转逻辑以迭代播放器消息,将空格作为拆分器,然后检查该单词是否在您的发誓集合中?这将给予控制

#5


0  

If you are using Java 8 you can use following code

如果您使用的是Java 8,则可以使用以下代码

String message = event.getMessage(); // When a player sends a message to the server, this is his message.
if (aswear.stream().anyMatch(x -> x.equalsIgnoreCase(message.trim()))) {
    event.setCancelled(true);
    event.getBukkitPlayer().sendMessage(PredefinedMessages.PHOENIX_SWEARING_DETECTED.build());
}

#6


0  

What you could do, would be to split the player message after whitespaces, and then check all words in the message against the list of swears. Then if the message words are identical, or however you design the likeness percentage, then you block the word,e.g. replace it with something else.

您可以做的是将空格后的播放器消息拆分,然后根据发誓列表检查消息中的所有单词。然后,如果消息词是相同的,或者你设计相似百分比,那么你阻止这个词,例如。用别的东西代替它。

Of course this works only for languages where the words in a sentence are delimited by whitespaces.

当然,这仅适用于句子中的单词由空格分隔的语言。

#7


0  

Split your message on words.

在单词上拆分你的信息。

String[] words = message.split("\\b");

Then check if any of the words are in the banned words list.

然后检查是否有任何单词在禁止的单词列表中。

boolean badWord = false;
for(String word: words){

    if(aswear.contains(word.toLowerCase())){
        badWord = true;
        break;
    }

}

#1


0  

Use message.split("\\b") to get an array of all "words" in the message. For each word, check if your blacklist contains that word:

使用message.split(“\\ b”)获取消息中所有“单词”的数组。对于每个单词,检查您的黑名单是否包含该单词:

String[] msgWords = message.split("\\b");
for (String word : msgWords) {
  if (aswear.contains(word.toLowerCase(Locale.US))) {
    // do whatever has to be done
    break;
  }
}

EDIT: Used \\b as word boundary regex, which really simplifies things (slightly stolen from Kent's answer).

编辑:使用\\ b作为单词边界正则表达式,这真的简化了事情(肯特的答案略有被盗)。

#2


3  

use regex check instead of String.contains. Then you can match word-boundary using \b

使用正则表达式检查而不是String.contains。然后你可以使用\ b匹配字边界

#3


0  

How about to define the array as " ass " with spaces around the words and use the same logic?

如何将数组定义为“ass”,并在单词周围添加空格并使用相同的逻辑?

#4


0  

How about a reversing the logic to iterate over player message with space as splitter and then checking whether that word is in your swear collection? That will give control

如何反转逻辑以迭代播放器消息,将空格作为拆分器,然后检查该单词是否在您的发誓集合中?这将给予控制

#5


0  

If you are using Java 8 you can use following code

如果您使用的是Java 8,则可以使用以下代码

String message = event.getMessage(); // When a player sends a message to the server, this is his message.
if (aswear.stream().anyMatch(x -> x.equalsIgnoreCase(message.trim()))) {
    event.setCancelled(true);
    event.getBukkitPlayer().sendMessage(PredefinedMessages.PHOENIX_SWEARING_DETECTED.build());
}

#6


0  

What you could do, would be to split the player message after whitespaces, and then check all words in the message against the list of swears. Then if the message words are identical, or however you design the likeness percentage, then you block the word,e.g. replace it with something else.

您可以做的是将空格后的播放器消息拆分,然后根据发誓列表检查消息中的所有单词。然后,如果消息词是相同的,或者你设计相似百分比,那么你阻止这个词,例如。用别的东西代替它。

Of course this works only for languages where the words in a sentence are delimited by whitespaces.

当然,这仅适用于句子中的单词由空格分隔的语言。

#7


0  

Split your message on words.

在单词上拆分你的信息。

String[] words = message.split("\\b");

Then check if any of the words are in the banned words list.

然后检查是否有任何单词在禁止的单词列表中。

boolean badWord = false;
for(String word: words){

    if(aswear.contains(word.toLowerCase())){
        badWord = true;
        break;
    }

}