Home > Enterprise >  Kotlin, why is function deprecated?
Kotlin, why is function deprecated?

Time:10-21

Recently, i've wanted to use capitalize() function. When I did so, a warning occured:

'capitalize(): String' is deprecated. Use replaceFirstChar instead.

The suggested replaceFirstChar feels quite long and complicated:

input.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }

Although everything works fine with capitalize(), i've been just wondering what the reason for deprecated warning is, if there's any problem with it and how can i solve this.

CodePudding user response:

There is no problem with the code per se. The "issue" is what capitalize means for all of the possible languages/characters/platforms/locales.

The doc even says why it is deprecated.

The title case of a character is USUALLY the same as its upper case with several exceptions. The particular list of characters with the special title case form depends on the underlying platform.

If you follow all of the reasoning why it is deprecated you reach isTitleCase where you will have an example of characters in languages where the character should not be "upper cased".

Capitalization is writing a word with its first letter as a capital letter (uppercase letter) and the remaining letters in lower case, in writing systems with a case distinction.

There are a few good examples and explanations for different languages on Letter case Wiki e.g.

The German letter "ß" formerly existed only in lower case.

  • Related