どうも、ちょげ(@chogetarou)です。
replcaeFirstChar()関数を使って文字列の先頭の文字を大文字に変換する方法を紹介します。
方法

replcaeFirstChar()関数を使って文字列の先頭の文字を大文字に変換するには、uppercase()を使います。
まず、文字列からreplaceFirstChar{}を呼び出します。
そして、replcaeFirstChar{}のクロージャーで、itからuppercase()を呼び出します。
val result = text.replaceFirstChar { it.uppercase() }
上記のreplaceFirstChar()は、呼び出した文字列の最初の文字を大文字に変換した文字列を返します。
使用例
fun main() {
val text = "hello"
val result = text.replaceFirstChar { it.uppercase() }
println(text)
println(result)
}
出力:
hello
Hello
コメント