How to convert a string to lower and uppercase in Scala

Overview

The methods toUpperCase and toLowerCase are used to convert strings to upper and lowercase respectively. The methods take a string and either convert all the letters to upper or lowercase.

The following is the definition of toUpperCase in Scala:

String toUpperCase()

The following is the definition of toLowerCase in Scala:

String toLowerCase()

Parameters

Both toUpperCase and toLowerCase do not take any input parameters.

Return value

toUpperCase returns a string in all uppercase letters, and toLowerCase returns a string in all lowercase letters.

Example

The following is an example of how you can use the methods toUpperCase and toLowerCase to convert strings:

object Main extends App {
val str = "Hello World!"
val lowerstr = str.toLowerCase()
val upperstr = str.toUpperCase()
print("In lowercase: " + lowerstr + "\n")
print("In uppercase: " + upperstr)
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved