Methods such as String.indexOf
and String.lastIndexOf
allow you to search for an occurrence of either a single char or a substring within a String
. If you'd like to search for an occurrence of a single char, it is recommended that you use the appropriate method that takes a Char
as a parameter rather than a String
as the former approach is more performant and recommended.
val lang = "Scala"
// Searching for `a` using the method that takes a String as a parameter.
// Although this works, it is not an efficient approach.
val idx = lang.indexOf("a")
val lang = "Scala"
// Searching for `a` using the method that takes a Char as a parameter.
// This is the recommended performant approach.
val idx = lang.indexOf('a')