How to case-insensitively check if a character exists in a string with Kotlin

1 Answer

0 votes
// Case-insensitive check without a loop
fun charExistsIgnoreCase(s: String, target: Char): Boolean {
    // Convert both to lowercase and use contains()
    return s.lowercase().contains(target.lowercase())
}

fun main() {
    // Define the string we want to search in
    val s = "KotlinLanguage"

    // Perform the case-insensitive check
    val exists = charExistsIgnoreCase(s, 'k')

    // Print the raw boolean result
    println(exists)

    // Conditional check
    if (exists) {
        println("exists")
    } else {
        println("not exists")
    }
}


/*
run:

true
exists

*/

 



answered Jun 26 by avibootz

Related questions

...