Kotlin:
Capitalizing a string

How to:

In Kotlin, strings can be capitalized using the standard library functions without the need for third-party libraries. Kotlin’s approach to handling strings makes these operations straightforward and concise.

Capitalizing the entire string:

val message = "hello, world!"
val capitalizedMessage = message.uppercase()

println(capitalizedMessage) // Output: HELLO, WORLD!

Capitalizing only the first character:

As of Kotlin 1.5, the capitalize() function is deprecated and replaced with a combination of replaceFirstChar and a lambda that checks if it is a lower case letter to transform it to uppercase.

val greeting = "hello, world!"
val capitalizedGreeting = greeting.replaceFirstChar {
    if (it.isLowerCase()) it.titlecase() else it.toString()
}

println(capitalizedGreeting) // Output: Hello, world!

This approach maintains the rest of the sentence in its original form while only changing the first letter to uppercase.