0

Alright So I am trying to check and see if a string exists but I don't know what case it's going to be in so How would I use it in my method?

here is my code

    if (!p.hasPermission("core.chat.curse")){
    String message = e.getMessage().toString().toLowerCase().replace(" ", "");
    List<String> cursewords = manager.getCurseFile().getStringList("cursewords");

    for (String badword : cursewords){
        badword = badword.toString().toLowerCase();
    if (message.contains(badword)){
        String crossout = "";

        for (int x = 0; x < badword.length(); x++){
            crossout += "*";

        }
        e.setMessage(e.getMessage().replace(badword, crossout)); //I need to 
                    //replace Ignore Case Here
        }
    }
}

.replace does not take a regex, so I can't use that!

How would I go about doing this, because the player can input the word in any case?

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
user253596
  • 21
  • 4
  • You could convert the bad words to `lower case`, then convert the player's chat message to `lower case`, and check that way. – Jojodmo Mar 22 '14 at 17:15

1 Answers1

1

You would want to use a case insensitive replacement regex like so:

if (message.contains("(?i)"+badword))
Reuben Tanner
  • 5,229
  • 3
  • 31
  • 46
  • I am already to check and see if it contains it I am just trying to replace the method and the replace method doesn't take a regex so that isn't going to work – user253596 Mar 22 '14 at 01:46
  • You're right in that replace does not take a regex but replaceAll does. Use that one. Also, make sure you remove your toLowerCase call above. – Reuben Tanner Mar 22 '14 at 01:49