When using Kotlin 1.5, Android Studio warns that String.capitalize
is deprecated.
The suggested replacement is:
myString.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() })
Why is the check for isLowerCase
neccessary?
Why can't I just do this:
myString.replaceFirstChar { it.titlecase(Locale.getDefault()) })
A quote from the ticket:
Below is the original answer based on my thoughts. Please also check comment as there is a good note regarding
ff
:I suppose this is because they wanted to replicate original behavior. Currently
capitalize
implemented as:Also, according to the note:
There are some corner cases:
Output (playground):