The Kotlin example shows how to get the first and last characters of a string. It uses indexing operations and alternative string methods.
Kotlin - Get First and Last Characters of a String Example
The example shows how to get the first and last characters of a string. It uses indexing operations and alternative string methods.
package net.javaguides.kotlin.examples
fun main(args: Array<String>) {
val s = "sourcecodeexamples.net"
println(s[0])
println(s[s.length-1])
println(s.first())
println(s.last())
}
Output:
s
t
s
t
The indexes start from zero; therefore, the first character has zero index. The index of the character is placed between square brackets.
println(s[0])
println(s[s.length-1])
The first() method returns the first and the last() returns the last character of the string:
println(s.first())
println(s.last())
Comments
Post a Comment