How to check if a string is title case in Scala

1 Answer

0 votes
object Main {
  def isTitleCase(s: String): Boolean = {
    s.split("\\s+").forall { word =>
      word.nonEmpty &&
      word.head.isUpper &&
      word.tail.forall(_.isLower)
    }
  }

  def main(args: Array[String]): Unit = {
    println(isTitleCase("Hello World"))   // true
    println(isTitleCase("Hello world"))   // false
  }
}


/*
run:

true
false

*/

 



answered 3 hours ago by avibootz
...