Solved Replace all letters of a swear word

Discussion in 'Plugin Development' started by stimoze, Jul 31, 2016.

Thread Status:
Not open for further replies.
  1. Im making a swear-control system for my plugin, and my idea was to replace all letters of the swear word to "*"-s.
    For example: The plugin changes the F word to ****

    This is my original code:
    Code:
            for (String word : e.getMessage().split(" "))
            {
                if (plugin.getConfig().getStringList("words").contains(word))
                {
                e.setMessage(e.getMessage().replaceAll(word, "*"));
                e.getPlayer().sendMessage("§cMessage contains cursewords! Action was aborted!");
                TitleAPI.sendTitle(e.getPlayer(), 2, 5, 2, "§8[§cAntiSwear-System§8]", "§c§lDon't swear!");
                }
            }
    What's the easiest/best way to do this?
     
  2. @stimoze
    I would take the word that you found, get the length of it and make a for loop running length times. And inside it have a StringBuilder each time append a *, and then replace the word with the stringbuilder string.
     
  3. Offline

    ArsenArsen

    .replaceAll(".", target)

    Boom. One line from 4 or 3.
     
    AlvinB likes this.
  4. @ArsenArsen
    Wops, I forgot that replaceAll() was regex based. Very clever solution if I do say so myself.
     
    ArsenArsen likes this.
  5. Do you want me to replace a character with a stringlist? :O XD

    Anyways @AlvinB your method worked, thank you :)
     
Thread Status:
Not open for further replies.

Share This Page