Kotlin:
Converting a string to lower case
How to:
Kotlin’s toLowerCase()
function turns all characters in a string to lower case quickly. Here’s how you use it:
fun main() {
val originalString = "ThiS iS A MixED cAsE String!"
val lowerCaseString = originalString.lowercase()
println(lowerCaseString) // Output: this is a mixed case string!
}
Invoke lowercase()
and you’re done. Input caps don’t matter; output’s all lower case.
Deep Dive
Kotlin didn’t reinvent the wheel for lower-casing strings. It’s actually a common feature across programming languages. Historically, functions like C’s tolower()
have long dealt with case conversion.
Now, two twists when lowercasing: locales and performance. Kotlin’s lowercase()
can accept a Locale
because, surprise, character casing isn’t universal. For instance, the Turkish dotted and dotless ‘I’ behave uniquely in case conversions.
Performance? In most apps, you won’t notice. But large-scale text processing hogs more memory and time because strings in Kotlin are immutable. When you lowercase a string, you get a new string.
Old-schoolers remember .toLowerCase()
— Kotlin now prefers lowercase()
for clarity.
See Also
- Kotlin String Documentation: Kotlinlang.org
- For text processing and advanced case manipulation, check the
java.lang.String
API: Oracle Docs - Understanding locales and language quirks: Oracle Locale Docs