Something like equalsIgnoreCase() but to ignore spaces?

Discussion in 'Spigot Plugin Development' started by TheMrJezza, May 28, 2015.

  1. Hey everyone, I'm making a chat filter and there are a lot of words I have to replace, but while I was testing it, I noticed that if players put a space anywhere in a banned word, that's close enough to saying that word and it won't be filtered. So instead of writing out every possible combination for every single word that's banned (a lot of them), is it possible to ignore spaces and characters like ';:"|/?( etc....?

    thanks in advance.
     
  2. Well, try to do the string replace method replace all spaces.

    Code (Text):
     .replaceAll("\\s+","")
    Removes all whitespaces and non visible characters such as tab, \n .
     
  3. .trim() wil also work.
     
    • Agree Agree x 1
    • Informative Informative x 1
  4. I have
    Code (Text):
    edit=message.toLowerCase().trim().replace("t e s t","****");
    "t e s t" is a banned word, but I didn't think I should post it here. But it only filters the word if the word has spaces between every letter.
     
  5. Uh. try use this:
    Code (Text):
    String trimmed =message.toLowerCase().trim().replace("test",  "****")
     
  6. Oh I see it's easy to detect if the word test with spaces it's there the thing it's replacing it because you don't know the initial state of the word with the spaces, a solution I see is having a string wich would be the message with out spaces, if it contains "test" then iterate throught the message, if you find a letter like the first one of the word like "t" then start saving, if it follows by an space or the next letter of the word continue saving, then after finding the last letter you have the world to replace for **** saved.
    I hope you understood me. I can try explaining it again if you didnt.
    Because of course you need to save all the spaces in the message to resend it.

    Pseudocode example:
    ArrayList<String> bannedWords = new ArrayList<String>();
    bannedWords.add("TEST");
    String initialMessage = "This is a T E S T";
    String noSpaces = initialMessage.replace(" ", "");
    ArrayList<String> contaniningWords = new ArrayList<String>();
    for(String toCheck : bannedWords){
    if(noSpaces.contains(toCheck)){
    contaniningWords.add(toCheck);
    }
    }
    if(containingWords.size() > 0){
    for(String toCheck : containingWords){
    String toReplace = "";
    for(Iterate throught all the letters of initialMessage and toCheck){
    if(currentLetter.equals(currentLetterOfTheWord) || currentLetter.equals(" ")){
    toReplace + = currentLetter;
    if(letterInt == lastLetterInt){
    break;
    }
    }else{
    toReplace = "";
    }
    }
    initialMessage.replace(toReplace);
    }
    }
    You can also count how many times do the noSpaces message contains the word and iterate throught the message as many times as the count.
    Psdata: I don't know if this is the best way but I think it's a way.
     
    #6 pollitoyeye, May 28, 2015
    Last edited: May 28, 2015
  7. @pollitoyeye Honestly, this is the first time I have ever tried anything that involves what a player says, so if you wouldn't mind explaining your last post to me as if you were teaching this to a complete newb. I understand if you don't, but I am having trouble understanding.

    Edit: I posted this before you edited your post.
     
  8. I added pseudocode to the message.
    You'll may understand it better reading the pseudocode.
     
  9. @pollitoyeye so for
    Code (Text):
    for(Iterate throught all the letters of initialMessage and
    what do I put there?

    Edit: I can see how this seems like a stupid question...
     
  10. int wordSize = word.length();
    char currentCharacter = string.charAt(currentPosition);
    Try getting it yourself.
     
  11. I would probably just String#split(" "); saved onto a String[] type object and compare with that.
     
  12. Will not block players writing "SH IT" or "Sh1t";
     
  13. Then the complicated way with ArrayList<String> containing all characters using for(String s : thatObject){} and comparing then to see if they equal anything in the badwords List ._. Am so tired this probably isn't even correct
     
  14. trim() only removes spaces from the beginning or the end

    " ab" -> "ab"
    "ab " -> "ab"
    "a b" -> "a b"
     
  15. Simply use .replace(" ", "") and you removed all spaces. Then do .toLowerCase() and search for your words with .contains(str). This way you cant replace your words with stars, but you can block the message.
     
  16. If you use a split you just get "HE" "LLO" and not Hello to simply use the method contains, the rest of the code was to replace the words with spaces in the code.
    My code is what @Friwi said but with the part of replacing the words in the final message
     
  17. I was reffering to
    int someInt = 0;
    String word = "";
    for(String s : theList){
    someInt++;
    word += s;
    for(String bw : bannedWords){
    if(bw.equalsIgnoreCase(s)){
    //then do whatever
    }
    }
    and use that someInt variable to get words within whatever range because someone could bypass with some_swear_word or s1o1m1e swear word and such.
     
  18. I would suggest not using for(String bw : bannedWords), but rather a HashSet. This way you can use HashSet#contains(Object) to check if the word is banned. Not only does this clean up your code, it also makes it more efficient :)
     
  19. string.replace(" ", "");
     
  20. @JPG2000 I have tried this, but it will remove every space in the chat.