We check char equality using the equals()
function. If not ignoring case, then we can use the equality operator ==
also. It invokes the equals()
function only :
fun main() {
println('A'.equals('a')) // false
// OR
println('A' == 'a') // false
println('A'.equals('a', ignoreCase = true)) // true
}
Lets implement this function on our own. We can follow the below steps in order to achieve this :
fun Char.equals(other: Char, ignoreCase: Boolean = false): Boolean {
return if (ignoreCase) {
// convert both to same case and compare
lowercaseChar().code == other.lowercaseChar().code
} else {
// compare directly
this.code == other.code
}
}