whats the easiest way to make the first character ...
# getting-started
l
whats the easiest way to make the first character of a string uppercase, but leaving the rest as-is ? need an extension function similar to
.uppercase()
i came up with
fun String.foo() = this[0].uppercase() + drop(1)
, is that already the best it can be ?
s
You can use the replaceFirstChar extension combined with the .uppercase() extension.
"kotlin".replaceFirstChar { it.uppercase() }
2
l
thanks
👍 1
e
The recommended replacement for the deprecated
.capitalize()
extension is
.replaceFirstChar { it.titlecase() }
, not
.uppercase()
, because it does make a difference: https://pl.kotl.in/NbGTR5LaB
Copy code
'ß'.uppercase() == "SS"
'ß'.titlecase() == "Ss"
👍 2