How to check if a character exists in a string with Kotlin

1 Answer

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

    // Check for a character using contains()
    val hasK = s.contains('k')   // true
    val hasZ = s.contains('z')   // false

    // Print the raw boolean results
    println(hasK)
    println(hasZ)

    // Conditional check
    if (s.contains('k')) {
        println("exists")
    } else {
        println("not exists")
    }
}



/*
run:

true
false
exists

*/

 



answered 10 hours ago by avibootz
...