How to ignore sentence and case in Java .startsWith

0

This is my code:

if(e.getMessage().startsWith("/reciclar") || e.getMessage().startsWith("/ereciclar")){
e.setCancelled(true);
p.sendMessage("§cComando Bloqueado/Em Manutenção!");
}

But if you use / RECYCLE uppercase or in a different sentence Type / ReciClaR works as well, I want to know if to ignore cases in the startsWith in a way that enters the if ()

    
asked by anonymous 30.07.2017 / 17:46

2 answers

1

Java does not actually have a startsWith that ignores uppercase and lowercase as it has for equals , which is equalsIgnoreCase .

However, it's easy to come up with a logic that can do this by converting to uppercase or lowercase before comparing:

String msgMinuscula = e.getMessage().toLowerCase();

if(msgMinuscula.startsWith("/reciclar") || msgMinuscula .startsWith("/ereciclar")){
    e.setCancelled(true);
    p.sendMessage("§cComando Bloqueado/Em Manutenção!");
}

However, it is necessary to ensure that what is placed in startsWith is in lowercase also, otherwise it will be necessary to convert manually if it is a String placed by hand or calling the toLowerCase method if it is a variable

    
30.07.2017 / 18:04
1

Try to do as below:

e.getMessage().trim().equalsIgnoreCase("/reciclar")

    
31.07.2017 / 18:04