Hack Forums
[Java] Censoring - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] Censoring (/showthread.php?tid=3447678)



Censoring - ‌‌‌‌‌‌‌‌‌‌‌‌Acad3m1cM1n3r - Apr 30, 2013

Code
@SuppressWarnings("deprecation")
public class PlayerListener implements Listener {

    private AdminPlugin plugin;

    public PlayerListener(AdminPlugin pl) {
        plugin = pl;
    }

    String[] disapprovedWords = { "fag", "shit", "bitch", "cock", "fuck",
            "cunt", "whore", "penis", "vagina", "nice person", "nigga", "faggot" };
    String replacement = "*****";

    @EventHandler
    public void onPlayerChat(PlayerChatEvent event) {
        String sentMessage = event.getMessage();
        String censoredString = null;
        if (plugin.isCensored()) {

            for (String s : disapprovedWords) {

                censoredString = censoredString.replaceAll(s, replacement);
            }
            event.setMessage(censoredString);
        }
        if (plugin.getHiddenPlayers().contains(event.getPlayer())) {
            event.setCancelled(true);
        }
    }
}

I'm trying to censor the words in the disapprovedWords array but I can't remember how to make it ignore the case.
I tried using toLowerCase() but then the whole message is lowercase, which is pretty annoying.

Help? :/


* - Emotional Patrick - Apr 30, 2013

Use some simple regex with the Matcher and Pattern classes.


* - GamingManiac - Apr 30, 2013

What about the Java.lang.String.equalsIgnoreCase(String str) function?

Would that work