Kotlin 2.1からこれまで警告(Warning)だった一部の非推奨APIがエラー(Error)扱いになりました。そのAPIをまとめます。
toLowerCase()
// 修正前
val name = "ANDROID".toLowerCase()
// 修正後
val name = "ANDROID".lowercase()
Localeを指定する場合
// 修正前
val name = text.toLowerCase(Locale.JAPAN)
// 修正後
val name = text.lowercase(Locale.JAPAN)
toUpperCase()
// 修正前
val name = "android".toUpperCase()
// 修正後
val name = "android".uppercase()
Locale指定の場合:
val name = "android".uppercase(Locale.JAPAN)
Char.toLowerCase()
StringではなくCharの場合は専用メソッドへ変更します。
// 修正前
val c = 'A'.toLowerCase()
// 修正後
val c = 'A'.lowercaseChar()
Char.toUpperCase()
大文字変換も同じです。
// 修正前
val c = 'a'.toUpperCase()
// 修正後
val c = 'a'.uppercaseChar()
StringBuilder.appendln()
// 修正前
val builder = StringBuilder()
builder.appendln("Hello")
builder.appendln("Kotlin")
// 修正後
val builder = StringBuilder()
builder.appendLine("Hello")
builder.appendLine("Kotlin")
capitalize()
// 修正前
val result = text.capitalize()
// 修正後
val result = text.replaceFirstChar {
if (it.isLowerCase()) {
it.titlecase()
} else {
it.toString()
}
}
decapitalize()
// 修正前
val result = text.decapitalize()
// 修正後
val result = text.replaceFirstChar {
it.lowercase()
}
移行したAPIまとめ
| 変更前 | 変更後 |
|---|---|
| toLowerCase() | lowercase() |
| toUpperCase() | uppercase() |
| Char.toLowerCase() | lowercaseChar() |
| Char.toUpperCase() | uppercaseChar() |
| appendln() | appendLine() |
| capitalize() | replaceFirstChar() |
| decapitalize() | replaceFirstChar() |