Start learning free

LESSON 66 OF 414 · STRINGS I

Uppercase and Lowercase in Scala: toUpperCase and toLowerCase

Lesson Read the full explanation

In Scala, String objects have built-in methods to change their case:

s.toUpperCase    // converts all characters to uppercase
s.toLowerCase    // converts all characters to lowercase

Here are some examples:

var word = "Hello World"
var upper = word.toUpperCase  // "HELLO WORLD"
var lower = word.toLowerCase  // "hello world"

var name = "ALICE"
println(name.toLowerCase)  // "alice"

These methods return a new String — the original is not modified.


Assignment
Follow the Coding Tutorial and convert word to both uppercase and lowercase!


Hint
Use word.toUpperCase and word.toLowerCase.

STEP 1 OF 2
    Ctrl + Enter to check

    SCALA · STRINGS I

    String Upper Lower in Scala, explained

    Changing the case of a string

    Two methods convert every letter of a string and return a new string:

    val word = "Hello World"
    val upper = word.toUpperCase
    val lower = word.toLowerCase
    
    println(upper) // HELLO WORLD
    println(lower) // hello world
    println(word)  // Hello World
    

    The original is unchanged, since strings are immutable. Characters that aren't letters, such as spaces and digits, pass through untouched.

    With or without parentheses

    These methods come from Java, where they are written toUpperCase(). Scala 3 accepts both forms for a method like this, so word.toUpperCase and word.toUpperCase() give the same result. The lesson uses the form without parentheses.

    The exercise, step by step

    The lesson gives you val word = "Hello World".

    1. Uppercase. Declare a val named upper and assign word.toUpperCase. It holds HELLO WORLD.
    2. Lowercase. Declare a val named lower and assign word.toLowerCase. It holds hello world.
    val word = "Hello World"
    
    val upper = word.toUpperCase
    val lower = word.toLowerCase
    

    Where it helps

    Comparing text without caring about case is the most common use. Convert both sides first, or use equalsIgnoreCase:

    println("Hello".toLowerCase == "hELLO".toLowerCase) // true
    println("Hello".equalsIgnoreCase("hELLO"))          // true
    println("Hello World".toLowerCase.indexOf("world")) // 6
    

    The last line shows case-insensitive searching: lowering the text first lets indexOf find world.

    Gotchas

    • Using the result. A line that is only word.toUpperCase does nothing visible. Assign or print the value.
    • Not always one-to-one. Some letters change length: "straße".toUpperCase is STRASSE, so the new string can be longer.
    • Capitalising a word. To uppercase only the first letter, use "hello".capitalize, which gives Hello.
    • Single characters. A Char has toUpper and toLower, so 'a'.toUpper is 'A'.
    • Chaining. You can combine calls, for example " Hi ".trim.toUpperCase.

    Why case conversion matters

    Computers treat H and h as different characters, so "Hello" == "hello" is false. When you accept text from people, they will type it in all sorts of ways: YES, yes, Yes. Converting to a single case first means your code only needs to handle one spelling.

    Case conversion is also handy for output. You can show a heading in capitals with toUpperCase, or normalise a list of names to lowercase before sorting them so that capitalised names don't all sort before lowercase ones.

    Remember that the conversion is for comparison and display. Keep the original string if you need to show the user exactly what they typed, and use the converted copy only for the check.

    Practising with the exercise

    After you finish, try printing both new values together with the original. Seeing HELLO WORLD, hello world and Hello World side by side confirms that the two calls started from the same untouched string, and that you didn't convert the result of the first call by accident. Try a string with digits and punctuation too, such as "Room 101!": it becomes ROOM 101! and room 101!, with only the letters changing.

    Chaining with other string methods

    Because each method returns a string, you can chain calls in one expression. Cleaning up input before comparing it is a typical case: " Hi ".trim.toUpperCase first removes the outer spaces and then converts the rest to HI. The calls run from left to right, so the order can matter. Reading the chain aloud as a list of steps makes it easier to check.

    Key takeaways

    • toUpperCase and toLowerCase return new strings.
    • The original string stays as it was.
    • Convert both sides before comparing text without regard to case.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free