Scala - How to capitalize every word in a string? Code Example

The code for How to capitalize every word in a string?

object Example {
    def main(args: Array[String]) = {
        var str = "hello, world!"
        var result = str.split(" ").map(_.capitalize).mkString(" ")
        println(result)

        str = "Do you have any specific compiler requirements?"
        result = str.split(" ").map(_.capitalize).mkString(" ")
        println(result)

        str = "serving the programmers since 2015"
        result = str.split(" ").map(_.capitalize).mkString(" ")
        println(result)        
    }
}    

/*
Output:
Hello, World!
Do You Have Any Specific Compiler Requirements?
Serving The Programmers Since 2015
*/
Code by IncludeHelp, on August 13, 2022 22:28

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.