To convert the first character in a string to uppercase in Kotlin, you can use the capitalize()
method of the String class.
In this example,
str1
with a value of "hello world"
.capitalize()
method on str1
to convert the first character to uppercase.fun main() {
val str1 = "hello world"
val capitalizedStr1 = str1.capitalize()
println("Modified string is: $capitalizedStr1")
}
Modified string is: Hello world
In this example,
str2
with a value of "java"
.capitalize()
method on str2
to convert the first character to uppercase.fun main() {
val str2 = "java"
val capitalizedStr2 = str2.capitalize()
println("Modified string is: $capitalizedStr2")
}
Modified string is: Java
In this tutorial, we learned How to convert First Character in String to Uppercase in Kotlin language with well detailed examples.