kotlin 文字列の先頭だけ大文字に変更する方法

環境
Windows10 Home 64bit
Java 19.0.1
Kotlin 1.7.20-release-201

構文
文字列.replaceFirstChar { it.uppercase() }
「replaceFirstChar { it.uppercase() }」関数で文字列の先頭だけ大文字に変更します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
fun main() {
println( "stu".replaceFirstChar { it.uppercase() } )
println( "sTU".replaceFirstChar { it.uppercase() } )
println( "abc".replaceFirstChar { it.uppercase() } )
}
fun main() { println( "stu".replaceFirstChar { it.uppercase() } ) println( "sTU".replaceFirstChar { it.uppercase() } ) println( "abc".replaceFirstChar { it.uppercase() } ) }
fun main() {
   
    println( "stu".replaceFirstChar { it.uppercase() } ) 

    println( "sTU".replaceFirstChar { it.uppercase() } ) 

    println( "abc".replaceFirstChar { it.uppercase() } )     
}

実行結果
Stu
STU
Abc

Kotlin

Posted by arkgame