How to ignore sentence and case in Java . startsWith

Asked

Viewed 180 times

1

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 capital or a different sentence Type /Recycle works too, I wonder if you can ignore cases in startsWith in a way that enters the if()

  • You want to check if the beginning of a string starts with /reciclar, independent of the text case, that’s it?

  • Yes, because if I put myself equalsIgnoreCase would have way to add a space after the command and would work the same way

  • It is not very clear your doubts, please edit the question and explain a little more

  • I think I solved it, I put it up for every time someone tries to use a command it automatically decreases the letters

  • You can answer with the solution below then :)

2 answers

1

Try to do as below:

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

1

Java doesn’t really have a startsWith that ignores capital letters and minuscules as it has for the equals, which is the equalsIgnoreCase.

However it is easy to find a logic that can do this by converting to uppercase or minuscule before comparing:

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

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

It is necessary however to ensure that what is placed on the startsWith is in minuscules as well, otherwise it will be necessary to convert manually if it is a String hand-held or calling the method toLowerCase if it is a variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.