Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin equivalent of Java's equalsIgnoreCase

Tags:

android

kotlin

What is the equivalent of Java equalsIgnoreCase in Kotlin to compare String values?

I have used equals but it's not case insensitive.

like image 926
Farwa Avatar asked May 06 '18 09:05

Farwa


People also ask

How do you check the Equal ignore case in Kotlin?

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.

How do you make a string case-insensitive in Kotlin?

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.

How do you remove spaces from a string in Kotlin?

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.


2 Answers

You can use equals but specify ignoreCase parameter:

"example".equals("EXAMPLE", ignoreCase = true) 
like image 157
hluhovskyi Avatar answered Oct 13 '22 05:10

hluhovskyi


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     } } 
like image 39
Hitesh Dhamshaniya Avatar answered Oct 13 '22 05:10

Hitesh Dhamshaniya