Kotlin – Delete First Character in String

Delete first character in String in Kotlin

To delete the first character in a given string in Kotlin, you can use String.substirng() function. The string without the first character is same as the substring from index=1 till the end of the string.

Find the substring from index=1 till the end of the string.

str.substring(1)

Examples

Delete first character in the message

In the following program, we take a message as string in str and delete the first character in the string.

Kotlin Program

fun main() {
    val str = "abcdefghijk"
    val resultString = str.substring(1)
    println(resultString)
}

Output

bcdefghijk

Explanation

a  b  c  d  f  g  h  i  j  k  <- str
0  1  2  3  4  5  6  7  8  9  <- indices

a  <- delete first character

   b  c  d  f  g  h  i  j  k
   1  2  3  4  5  6  7  8  9
   str(1)