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?

1
  • You could convert the bad words to lower case, then convert the player's chat message to lower case, and check that way. Commented Mar 22, 2014 at 17:15

1 Answer 1

1

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

if (message.contains("(?i)"+badword)) 
Sign up to request clarification or add additional context in comments.

2 Comments

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
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.