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

1 Answer

0 votes
object CheckCharExists extends App {

  // Define the string we want to search in
  val s = "scalalang"

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

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

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


/*
Output:

true
false
exists

*/

 



answered 1 hour ago by avibootz
...