KT-38817 capitalize uses title case for the first char where available

This unifies its behavior with new capitalize overload with Locale.

Co-authored-by: Abduqodiri Qurbonzoda <abduqodiri.qurbonzoda@jetbrains.com>
This commit is contained in:
Ilya Gorbunov
2020-06-07 20:35:11 +03:00
parent 9e2f95233c
commit 33150a0809
4 changed files with 62 additions and 17 deletions
@@ -569,19 +569,24 @@ public inline fun String.toPattern(flags: Int = 0): java.util.regex.Pattern {
}
/**
* Returns a copy of this string having its first letter uppercased, or the original string,
* if it's empty or already starts with an upper case letter.
* Returns a copy of this string having its first letter titlecased using the rules of the default locale,
* or the original string if it's empty or already starts with a title case letter.
*
* 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.
*
* @sample samples.text.Strings.capitalize
*/
public actual fun String.capitalize(): String {
return if (isNotEmpty() && this[0].isLowerCase()) substring(0, 1).toUpperCase() + substring(1) else this
return capitalize(Locale.getDefault())
}
/**
* Returns a copy of this string having its first letter titlecased preferring [Char.toTitleCase] (if different from
* [Char.toUpperCase]) or by [String.toUpperCase] using the specified [locale], or the original string,
* if it's empty or already starts with an upper case letter.
* Returns a copy of this string having its first letter titlecased using the rules of the specified [locale],
* or the original string if it's empty or already starts with a title case letter.
*
* 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.
*/
@SinceKotlin("1.4")
@WasExperimental(ExperimentalStdlibApi::class)
@@ -605,17 +610,17 @@ public fun String.capitalize(locale: Locale): String {
}
/**
* Returns a copy of this string having its first letter lowercased, or the original string,
* if it's empty or already starts with a lower case letter.
* Returns a copy of this string having its first letter lowercased using the rules of the default locale,
* or the original string if it's empty or already starts with a lower case letter.
*
* @sample samples.text.Strings.decapitalize
*/
public actual fun String.decapitalize(): String {
return if (isNotEmpty() && this[0].isUpperCase()) substring(0, 1).toLowerCase() + substring(1) else this
return if (isNotEmpty() && !this[0].isLowerCase()) substring(0, 1).toLowerCase() + substring(1) else this
}
/**
* Returns a copy of this string having its first letter lowercased using the specified [locale],
* Returns a copy of this string having its first letter lowercased using the rules of the specified [locale],
* or the original string, if it's empty or already starts with a lower case letter.
*/
@SinceKotlin("1.4")