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

1 Answer

0 votes
object FirstfInstanceIndex {
  def main(args: Array[String]): Unit = {
    val s = "Scala Programming"
    val charToFind = 'm'

    val index = s.indexOf(charToFind)

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


 
 
/*
run:
   
The first occurrence of 'm' is at index 12
 
*/

 



answered Dec 29, 2024 by avibootz
...