Scala - How to capitalize each word in a string

Here’s a quick example of how to capitalize each word in a String using Scala:

scala> "employee salary".split(' ').map(_.capitalize).mkString(" ")
res2: String = Employee Salary

The way this works is that you split the given String into “words” with split, which creates an Array[String]; then capitalize each word in the array with map; then convert the array back to a String.