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".
- Uppercase. Declare a
valnamedupperand assignword.toUpperCase. It holdsHELLO WORLD. - Lowercase. Declare a
valnamedlowerand assignword.toLowerCase. It holdshello 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.toUpperCasedoes nothing visible. Assign or print the value. - Not always one-to-one. Some letters change length:
"straße".toUpperCaseisSTRASSE, so the new string can be longer. - Capitalising a word. To uppercase only the first letter, use
"hello".capitalize, which givesHello. - Single characters. A
CharhastoUpperandtoLower, so'a'.toUpperis'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
toUpperCaseandtoLowerCasereturn new strings.- The original string stays as it was.
- Convert both sides before comparing text without regard to case.
