To get the first character of a given string in Kotlin, you can use the indexing operator or the get()
method of the String class.
In this example,
str
with a value of "Hello World"
.get()
method with index 0 on the string str
.fun main() {
val str = "Hello World"
val firstChar = str[0]
println("First character: $firstChar")
}
First character: H
In this example,
str
with a value of "apple"
.get()
method with index 0 on the string str
.fun main() {
val str = "apple"
val firstChar = str.get(0)
println("First character: $firstChar")
}
First character: a
In this tutorial, we learned How to get First Character in String in Kotlin language with well detailed examples.