What is the equivalent of Java equalsIgnoreCase
in Kotlin to compare String
values?
I have used equals
but it's not case insensitive.
ignoreCase - true to ignore character case when comparing strings. By default false . Returns true if this character is equal to the other character, optionally ignoring character case. Two characters are considered equal ignoring case if Char.
If you want to have a case insensitive comparison. Then just pass true as the second argument. Case-insensitive string comparison in kotlin, we can use the equals method and pass true as the second argument.
Since the string is immutable in Kotlin, it returns a new string having leading and trailing whitespace removed. To just remove the leading whitespaces, use the trimStart() function. Similarly, use the trimEnd() function to remove the trailing whitespaces.
You can use equals
but specify ignoreCase
parameter:
"example".equals("EXAMPLE", ignoreCase = true)
As per the Kotlin Documentation :
fun String?.equals( other: String?, ignoreCase: Boolean = false ): Boolean
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/equals.html
For Example:
val name: String = "Hitesh" when{ name.equals("HITESH", true) -> { // DO SOMETHING } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With