Kotlin How to Convert String to Uppercase Easily
In Kotlin, you can convert a string to uppercase by calling the
uppercase() function on the string, like myString.uppercase().Examples
Inputhello
OutputHELLO
InputKotlin is fun!
OutputKOTLIN IS FUN!
Input123abc
Output123ABC
How to Think About It
To convert a string to uppercase in Kotlin, think of changing every letter to its capital form. You use the built-in
uppercase() function which goes through each character and makes it uppercase if it can. This is simple and direct.Algorithm
1
Take the input string.2
Call the uppercase function on the string.3
Return the new string with all letters in uppercase.Code
kotlin
fun main() {
val original = "hello Kotlin"
val uppercased = original.uppercase()
println(uppercased)
}Output
HELLO KOTLIN
Dry Run
Let's trace the string "hello Kotlin" through the code
1
Original string
original = "hello Kotlin"
2
Convert to uppercase
uppercased = original.uppercase() -> "HELLO KOTLIN"
3
Print result
Output: HELLO KOTLIN
| Step | String Value |
|---|---|
| 1 | hello Kotlin |
| 2 | HELLO KOTLIN |
Why This Works
Step 1: Using uppercase() function
The uppercase() function is built into Kotlin strings and converts all letters to uppercase.
Step 2: Returns new string
It does not change the original string but returns a new string with uppercase letters.
Alternative Approaches
uppercase(Locale)
kotlin
fun main() {
val original = "hello Kotlin"
val uppercased = original.uppercase(java.util.Locale.getDefault())
println(uppercased)
}Use this if you want to consider locale-specific uppercase rules.
Complexity: O(n) time, O(n) space
Time Complexity
The function processes each character once, so time grows linearly with string length.
Space Complexity
A new string is created to hold the uppercase result, so space also grows linearly.
Which Approach is Fastest?
Using uppercase() without locale is fastest for general use; locale-aware versions add slight overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| uppercase() | O(n) | O(n) | Simple uppercase conversion |
| uppercase(Locale) | O(n) | O(n) | Locale-specific uppercase rules |
Use
uppercase() directly on your string for a quick uppercase conversion.Forgetting that
uppercase() returns a new string and does not modify the original string.