为什么要用在调用 titlecase 之前检查它是否为小写字母来替换 capitalize?
Why replace `capitalize` with a check for whether it is lowercase before `titlecase` is called?
使用 Kotlin 1.5 时,Android Studio 警告说 String.capitalize
已弃用。
建议的替换为:
myString.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() })
为什么需要检查 isLowerCase
?
为什么我不能这样做:
myString.replaceFirstChar { it.titlecase(Locale.getDefault()) })
引自ticket:
Additionally, users may have varied expectations about the behavior of fun String.capitalize(): String
, and the expectations do not always coincide with what we have implemented (see the KEEP discussion). The function titlecases the first char of the receiver String
only if it is a lower case letter. For instance, the result of "DŽ".capitalize()
is "DŽ"
, while "dž".capitalize()
is "Dž"
. Given the different expectations, we would like to introduce replaceFirstChar function to let users code exactly what they want.
The replacements are verbose, but preserve the behavior as close as possible. The resulting code can be simplified further if a more simple behavior is desired, for example to String.replaceFirstChar { it.uppercaseChar() }
.
以下是根据我的想法得出的原始答案。还请检查评论,因为关于 ff
:
有一个很好的说明
我想这是因为他们想复制原始行为。当前 capitalize
implemented 为:
public fun String.capitalize(locale: Locale): String {
if (isNotEmpty()) {
val firstChar = this[0]
if (firstChar.isLowerCase()) {
return buildString {
val titleChar = firstChar.titlecaseChar()
if (titleChar != firstChar.uppercaseChar()) {
append(titleChar)
} else {
append(this@capitalize.substring(0, 1).uppercase(locale))
}
append(this@capitalize.substring(1))
}
}
}
return this
}
另外,根据注释:
The title case of a character is usually the same as its upper case with several exceptions.
有一些极端情况:
fun check(x: String) {
println(x.capitalize())
println(x.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() })
println(x.replaceFirstChar { it.titlecase() })
}
fun main() {
check("dz")
check("DZ")
}
输出(playground):
Dz
Dz
Dz
DZ
DZ
Dz
使用 Kotlin 1.5 时,Android Studio 警告说 String.capitalize
已弃用。
建议的替换为:
myString.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() })
为什么需要检查 isLowerCase
?
为什么我不能这样做:
myString.replaceFirstChar { it.titlecase(Locale.getDefault()) })
引自ticket:
Additionally, users may have varied expectations about the behavior of
fun String.capitalize(): String
, and the expectations do not always coincide with what we have implemented (see the KEEP discussion). The function titlecases the first char of the receiverString
only if it is a lower case letter. For instance, the result of"DŽ".capitalize()
is"DŽ"
, while"dž".capitalize()
is"Dž"
. Given the different expectations, we would like to introduce replaceFirstChar function to let users code exactly what they want.
The replacements are verbose, but preserve the behavior as close as possible. The resulting code can be simplified further if a more simple behavior is desired, for example to
String.replaceFirstChar { it.uppercaseChar() }
.
以下是根据我的想法得出的原始答案。还请检查评论,因为关于 ff
:
我想这是因为他们想复制原始行为。当前 capitalize
implemented 为:
public fun String.capitalize(locale: Locale): String {
if (isNotEmpty()) {
val firstChar = this[0]
if (firstChar.isLowerCase()) {
return buildString {
val titleChar = firstChar.titlecaseChar()
if (titleChar != firstChar.uppercaseChar()) {
append(titleChar)
} else {
append(this@capitalize.substring(0, 1).uppercase(locale))
}
append(this@capitalize.substring(1))
}
}
}
return this
}
另外,根据注释:
The title case of a character is usually the same as its upper case with several exceptions.
有一些极端情况:
fun check(x: String) {
println(x.capitalize())
println(x.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() })
println(x.replaceFirstChar { it.titlecase() })
}
fun main() {
check("dz")
check("DZ")
}
输出(playground):
Dz
Dz
Dz
DZ
DZ
Dz