https://kotlinlang.org logo
#getting-started
Title
# getting-started
l

LastExceed

10/26/2021, 10:32 PM
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

Scott

10/26/2021, 10:38 PM
You can use the replaceFirstChar extension combined with the .uppercase() extension.
"kotlin".replaceFirstChar { it.uppercase() }
2
l

LastExceed

10/26/2021, 10:38 PM
thanks
👍 1
e

ephemient

10/26/2021, 11:04 PM
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
2 Views