Skip to main content

How to capitalize the first letter of each word in a string in Kotlin

How to capitalize the first letter of each word in a string in Kotlin.

Here's a step-by-step tutorial on how to capitalize the first letter of each word in a string in Kotlin.

Step 1: Create a function to capitalize the first letter

To capitalize the first letter of a word, we can create a function that takes in a string and returns the same string with the first letter capitalized. Here's an example:

fun capitalizeFirstLetter(word: String): String {
return word.substring(0, 1).toUpperCase() + word.substring(1)
}

In this function, we use the substring() method to extract the first letter of the word and convert it to uppercase using the toUpperCase() method. We then concatenate this capitalized letter with the rest of the word using the substring() method again.

Step 2: Create a function to capitalize the first letter of each word Now that we have a function to capitalize the first letter of a word, we can create another function that takes in a string containing multiple words and returns the same string with the first letter of each word capitalized. Here's an example:

fun capitalizeWords(sentence: String): String {
val words = sentence.split(" ")
val capitalizedWords = words.map { capitalizeFirstLetter(it) }
return capitalizedWords.joinToString(" ")
}

In this function, we first split the sentence into individual words using the split() method, which splits the string based on the provided delimiter (in this case, a space). We then use the map() function to apply the capitalizeFirstLetter() function to each word in the list, resulting in a list of capitalized words. Finally, we use the joinToString() method to combine the capitalized words back into a sentence, using a space as the separator.

Step 3: Test the function

To test our capitalizeWords() function, we can call it with a sample string and print the result. Here's an example:

fun main() {
val sentence = "hello world"
val capitalizedSentence = capitalizeWords(sentence)
println(capitalizedSentence)
}

When we run the above code, the output will be:

Hello World

This confirms that our capitalizeWords() function is working correctly.

Step 4: Handle edge cases

It's important to consider edge cases when working with string manipulation. For example, what should happen if the input string is empty or consists of only spaces? To handle such cases, you can add additional checks in your capitalizeWords() function.

Here's an updated version of the function that handles empty and whitespace-only strings:

fun capitalizeWords(sentence: String): String {
if (sentence.isEmpty()) {
return sentence
}

val words = sentence.trim().split("\\s+")
val capitalizedWords = words.map { capitalizeFirstLetter(it) }
return capitalizedWords.joinToString(" ")
}

In this updated version, we first check if the sentence is empty using the isEmpty() method. If it is, we simply return the empty string as is. We also use the trim() method to remove leading and trailing whitespace from the sentence, and then split the trimmed sentence using the regular expression "\s+" to handle multiple consecutive spaces as a single delimiter.

That's it! You now know how to capitalize the first letter of each word in a string in Kotlin.