When learning Kotlin, one of the first things you’ll often need to do is work with strings. Strings are everywhere in programming — from user input to displaying data, and even formatting messages. Sometimes you need to change the case of text, turning lowercase letters into uppercase, or the other way around. This process is called string case conversion. It may seem simple, but it’s a very important skill in writing clean and readable programs.
with hands-on learning.
get the skills and confidence to land your next move.
In real-world coding, case conversion comes up all the time. You may want to make a username lowercase before saving it to a database, show a title in uppercase for design purposes, or ensure consistent formatting in reports and user interfaces. In Kotlin, there are many ways to do this — some short and simple, others more detailed and creative. Let’s explore these different methods together, step by step, in a way that feels easy and natural to understand.
Program 1: Converting String to Uppercase Using Built-in Function
The simplest and most direct way to convert a string to uppercase in Kotlin is by using the built-in uppercase() function.
fun main() {
val text = "hello kotlin"
val upperText = text.uppercase()
println("Uppercase: $upperText")
}This small program takes the text "hello kotlin" and converts it to uppercase. Kotlin’s standard library provides the uppercase() function, which transforms every character in the string to its uppercase version. It’s a very beginner-friendly approach and is perfect when you want a quick and clean solution without loops or extra logic.
Program 2: Converting String to Lowercase Using Built-in Function
Just like uppercase conversion, you can easily make a string lowercase using the lowercase() function.
fun main() {
val text = "HELLO KOTLIN"
val lowerText = text.lowercase()
println("Lowercase: $lowerText")
}Here, the string "HELLO KOTLIN" becomes "hello kotlin". This method is simple and efficient, making it one of the most common ways to handle lowercase conversion in Kotlin. Beginners can rely on it when formatting data, especially when handling user inputs that may come in mixed cases.
Program 3: Changing Case Manually Using Loops
Sometimes, you might want to handle case conversion yourself using loops. This helps you understand what happens behind the scenes and gives you more control.
fun main() {
val text = "Kotlin Rocks!"
var result = ""
for (ch in text) {
result += if (ch.isUpperCase()) ch.lowercaseChar() else ch.uppercaseChar()
}
println("Swapped Case: $result")
}In this example, the program loops through each character in the string. If a character is uppercase, it changes it to lowercase; if it’s lowercase, it converts it to uppercase. The final result is a fun “swapped case” version of the original text. This approach is great for learners who want to understand how loops and conditional statements work together in Kotlin.
Program 3.1: Manually Converting to Uppercase
This program loops through each character in the string and converts it to uppercase, without using the built-in uppercase() function.
fun main() {
val text = "Kotlin Rocks!"
var result = ""
for (ch in text) {
result += if (ch.isLowerCase()) ch.uppercaseChar() else ch
}
println("Uppercase: $result")
}Here, each character is checked. If it’s a lowercase letter, it’s converted to uppercase; otherwise, it stays the same. This method is useful for learning how string manipulation works step by step, and it’s a good exercise for understanding loops and conditions in Kotlin.
Program 3.2: Manually Converting to Lowercase
Similarly, this program converts each character to lowercase using a loop.
fun main() {
val text = "Kotlin Rocks!"
var result = ""
for (ch in text) {
result += if (ch.isUpperCase()) ch.lowercaseChar() else ch
}
println("Lowercase: $result")
}This works in the same way but in reverse. Uppercase letters are turned into lowercase, while all other characters remain unchanged. Beginners will find this helpful for practicing character checks and understanding how data flows through loops.
Program 4: Converting String Case Using Recursion
Another interesting way to convert string cases is by using recursion. This method uses a function that calls itself repeatedly until all characters are processed.
fun main() {
val text = "HelloWorld"
println("Swapped Case (Recursive): ${swapCaseRecursively(text)}")
}
fun swapCaseRecursively(str: String): String {
if (str.isEmpty()) return ""
val firstChar = str[0]
val swapped = if (firstChar.isLowerCase()) firstChar.uppercaseChar() else firstChar.lowercaseChar()
return swapped + swapCaseRecursively(str.substring(1))
}Here, the recursive function checks each character and swaps its case, then calls itself for the rest of the string until nothing is left. While it’s not the most efficient way for large text, it’s a great learning tool. It helps you grasp how recursion works and deepens your understanding of string processing in Kotlin.
Program 4.1: Converting to Uppercase Using Recursion
This program converts all letters to uppercase recursively.
fun main() {
val text = "HelloWorld"
println("Uppercase (Recursive): ${toUpperCaseRecursively(text)}")
}
fun toUpperCaseRecursively(str: String): String {
if (str.isEmpty()) return ""
val firstChar = str[0]
val upperChar = if (firstChar.isLowerCase()) firstChar.uppercaseChar() else firstChar
return upperChar + toUpperCaseRecursively(str.substring(1))
}Here, each character is checked: if it’s lowercase, it’s converted to uppercase; otherwise, it stays the same. The function then calls itself for the rest of the string. This is useful for learning recursion step by step and seeing how a string can be built incrementally.
Program 4.2: Converting to Lowercase Using Recursion
This program converts all letters to lowercase recursively.
fun main() {
val text = "HelloWorld"
println("Lowercase (Recursive): ${toLowerCaseRecursively(text)}")
}
fun toLowerCaseRecursively(str: String): String {
if (str.isEmpty()) return ""
val firstChar = str[0]
val lowerChar = if (firstChar.isUpperCase()) firstChar.lowercaseChar() else firstChar
return lowerChar + toLowerCaseRecursively(str.substring(1))
}In this example, uppercase letters are changed to lowercase while all other characters remain unchanged. This helps beginners practice recursion while clearly seeing the effect of character checks and string building.
Program 5: Converting the First Letter of Each Word to Uppercase
One of the most common real-world tasks is making each word in a sentence start with an uppercase letter, also known as title case.
fun main() {
val text = "kotlin is fun to learn"
val words = text.lowercase().split(" ")
val titleCased = words.joinToString(" ") { it.replaceFirstChar { ch -> ch.uppercaseChar() } }
println("Title Case: $titleCased")
}This program splits the string into words, then changes the first letter of each word to uppercase. Finally, it joins them back together. It’s a very practical technique for formatting names, book titles, or display text. Beginners will find this example useful because it mixes string methods with lambda expressions in a simple way.
Program 6: Using Map Function for Elegant Case Conversion
Kotlin’s functional programming features allow you to perform conversions using the map function, which transforms each character of the string.
fun main() {
val text = "Functional Kotlin"
val swapped = text.map { if (it.isLowerCase()) it.uppercaseChar() else it.lowercaseChar() }.joinToString("")
println("Swapped Case using Map: $swapped")
}Here, the map function walks through every character, applies a transformation, and builds a new string. It’s elegant and clean, and many Kotlin developers prefer this approach for its simplicity and readability. It’s also efficient and easy to maintain in modern Kotlin projects.
Frequently Asked Questions (FAQ)
Before we wrap up, let’s answer a few common questions beginners often ask about converting string cases in Kotlin.
Q1: Can I change only part of a string to uppercase or lowercase?
Yes! You can use Kotlin’s substring functions to target a specific part of the string and then apply uppercase() or lowercase() on that portion before combining it back.
Q2: What’s the difference between toUpperCase() and uppercase()?
In older Kotlin versions, toUpperCase() was used, but now uppercase() is the preferred method because it supports modern Unicode standards and handles international characters better.
Q3: Is it faster to use built-in functions or write loops?
Built-in functions are optimized and generally faster. Loops are more educational and give you more control when you need custom behavior.
Q4: Can I convert a string case without creating a new variable?
Yes, you can directly print the result of the conversion, like println(text.uppercase()). However, storing it in a variable is useful when you need to reuse it later.
Q5: Does case conversion work with numbers or symbols?
No, only letters are affected. Numbers, punctuation, and spaces remain the same during case conversion.
Conclusion
String case conversion in Kotlin may look simple, but it’s a core skill that appears in almost every project. Whether you’re writing small scripts, mobile apps, or large systems, being comfortable with string manipulation makes your code cleaner and more flexible. You’ve now seen how to use built-in methods, loops, recursion, and functional approaches like map to handle case changes easily.
The best way to learn is to experiment. Try combining these methods, test them with different strings, and see what happens. With each try, you’ll get more comfortable with Kotlin and understand not just how it works, but why it’s such a powerful and enjoyable language to code in.




