Changing the case of a string
Kotlin strings have two functions for changing case: uppercase() converts every letter to upper case, and lowercase() converts every letter to lower case. Characters that are not letters, such as digits, spaces and punctuation, stay as they are.
val word = "Hello World"
val upper = word.uppercase() // HELLO WORLD
val lower = word.lowercase() // hello world
val name = "ALICE"
println(name.lowercase()) // alice
println("Hello, World 123!".uppercase()) // HELLO, WORLD 123!
Both functions return a new string. Strings are immutable, so the original keeps its value:
val word = "Hello World"
val upper = word.uppercase()
println(word) // Hello World
println(upper) // HELLO WORLD
If you call word.uppercase() and do not store or print the result, nothing visible happens.
The old names no longer work
Older Kotlin code and tutorials use toUpperCase() and toLowerCase(). Those were deprecated in favour of uppercase() and lowercase(), and in Kotlin 2.1 using them is a compile error that says 'fun String.toUpperCase(): String' is deprecated. Use uppercase() instead. So if you find the old names in an example, write the new ones.
The exercise
The lesson gives word and asks for two new values, one at a time.
- The uppercase version. Declare a
valnamedupperand assignword.uppercase(). It holds"HELLO WORLD". - The lowercase version. Declare a
valnamedlowerand assignword.lowercase(). It holds"hello world".
val word = "Hello World"
val upper = word.uppercase()
val lower = word.lowercase()
Both results are computed from word, which never changes. Neither call affects the other, so their order does not matter.
Comparing without caring about case
A common use is to compare text regardless of case. "Hello" == "hello" is false, because the two strings differ. You can convert both sides to the same case first, or let Kotlin ignore the case for you:
val input = "YES"
if (input.lowercase() == "yes") {
println("agreed")
}
println("Hello".equals("hello", ignoreCase = true)) // true
println("abc".contains("B", ignoreCase = true)) // true
The first form is easy to read. The ignoreCase form does the comparison in one call.
Capitalizing the first letter
There is no function that changes only the first letter, but replaceFirstChar does it. It takes a lambda that receives the first character and returns its replacement. Inside, it.uppercase() converts that character:
println("hello world".replaceFirstChar { it.uppercase() }) // Hello world
To capitalize every word, split the string, capitalize each piece and join them again:
val title = "hello world foo".split(" ").joinToString(" ") { w ->
w.replaceFirstChar { it.uppercase() }
}
println(title) // Hello World Foo
Characters have their own functions
A single Char also has case functions, with a small catch in the names. 'a'.uppercaseChar() returns a Char, 'A'. 'a'.uppercase() returns a String, "A". The string version exists because in some alphabets one character becomes several when converted: "ß".uppercase() is "SS", which is two characters. For the same reason the converted string can be longer than the original.
Asking about the case
Sometimes you do not want to change the case but to ask about it. Each Char has isUpperCase() and isLowerCase(), and you can combine them with string functions that take a condition:
val text = "Hello World"
println(text.count { it.isUpperCase() }) // 2
println(text.filter { it.isLowerCase() }) // elloorld
println(text.first().isUpperCase()) // true
The first line counts the capital letters, the second keeps only the lower-case letters (the space is neither, so it is dropped), and the third checks the first character.
Same result everywhere
These functions do not depend on the language settings of the computer the program runs on. Kotlin converts with fixed, language-neutral rules, so the same code gives the same answer on every machine. That is why they are the right choice for normalizing text before comparing it: lowering both sides, or raising both sides, makes "Hello World" and "hello world" equal. Do the conversion once and reuse the result if you compare against the same text many times.
Common mistakes
- Ignoring the result.
word.uppercase()by itself leaveswordunchanged. - Using the deprecated names.
toUpperCase()andtoLowerCase()do not compile in current Kotlin. - Comparing with
==and expecting case to be ignored."Hello" == "hello"isfalse. - Expecting a changed length. For ordinary English text the length stays the same, but a few letters, such as
ß, grow when upper-cased.
Key takeaways
s.uppercase()ands.lowercase()return new strings; the original is unchanged.- Non-letters are left as they are.
- Use
equals(other, ignoreCase = true)for case-insensitive comparison. replaceFirstChar { it.uppercase() }capitalizes the first letter.