How to convert a string to title case in Scala

1 Answer

0 votes
object TitleCaseConverter {
  def toTitleCase(str: String): String = {
    str.split(" ").map(_.capitalize).mkString(" ")
  }

  def main(args: Array[String]): Unit = {
    val str = "In the beginning there was nothing, which exploded."
    
    val titleCased = toTitleCase(str)
    
    println(titleCased)  
  }
}

  
     
/*
run:
  
In The Beginning There Was Nothing, Which Exploded.

*/

 



answered Apr 15, 2025 by avibootz
...