What is the equivalent of the String.equalsIgnoreCase()
method of Java in Kotlin?
What is the equivalent of the String.equalsIgnoreCase()
method of Java in Kotlin?
To perform this comparison in Kotlin, pass the "ignoreCase" parameter in the equals function. Here is an example:
val texto1 = "testando";
val texto2 = "TESTANDO";
var resultado = texto1.equals(texto2, ignoreCase = true);
println(resultado);
Methods that make sense to have this option, and not only equals()
do so through a parameter indicating ignore case ignore case ). See equals()
documentation . See also the Kotlin String
documentation. Note that most methods have this parameter.
At this point I do not know if I liked Kotlin's decision since Boolean parameters are usually accepted more or less universally as bad engineering . If you do not have a very good reason for this choice, I think it was a mistake. If you have a good reason it should be very well documented, which I did not find. And I love language .
fun main(args: Array<String>) {
println("Texto Em Caixa".equals("texto em caixa", ignoreCase = true));
println("Texto Em Caixa".equals("texto em caixa", ignoreCase = false));
}
See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .