How to get the first letter of each word in a string with Scala

3 Answers

0 votes
object Main extends App {
  val s = "scala javascript php c c++ python"
  
  println(s.split(" ").flatMap(_.headOption).mkString)
}


   
/*
           
run:
     
sjpccp

*/

 



answered Sep 19, 2024 by avibootz
0 votes
object Main extends App {
  val s = "scala javascript php c c++ python"
  
  println("""\w+""".r.findAllIn(s).map(_.head).mkString)
}


   
/*
           
run:
     
sjpccp

*/

 



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


   
/*
           
run:
     
sjpccp

*/

 



answered Sep 19, 2024 by avibootz
...