How to get the first word from a string in Scala

3 Answers

0 votes
object Main extends App {
  val s = "scala javascript php c c++ python"
  
  println(s.substring(0, s.indexOf(' ')))
}


   
/*
           
run:
     
scala

*/

 



answered Sep 19, 2024 by avibootz
0 votes
object Main extends App {
  val s = "scala javascript php c c++ python"
  
  println(s.split(" ").head)
}


   
/*
           
run:
     
scala

*/

 



answered Sep 19, 2024 by avibootz
0 votes
object Main extends App {
  val s = "scala javascript php c c++ python"
  
  println(s.takeWhile(_ != ' '))
}


   
/*
           
run:
     
scala

*/

 



answered Sep 19, 2024 by avibootz
...