0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kotlin 2.1からエラーになるAPIまとめ

0
Posted at

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()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?