How to get the index of the first instance of a character in a string with Kotlin

1 Answer

0 votes
fun main() {
    val s = "Kotlin cross-platform"
    val charToFind = 'o'

    val index = s.indexOf(charToFind)

    if (index != -1) {
        println("The first occurrence of '$charToFind' is at index $index")
    } else {
        println("'$charToFind' not found in the string")
    }
}
 

 
/*
run:

The first occurrence of 'o' is at index 1
 
*/

 



answered Dec 29, 2024 by avibootz
...