In this tutorial, you shall learn how to remove the first character in a given string in Kotlin, using String.drop() function, with examples.

Kotlin – Remove first character in String

To remove first character in string in Kotlin, you can use String.drop() function.

Call the drop() function on the string, and pass 1 as argument, since we want to remove only one character from the start of this string. The function returns the contents of this string with the first character removed.

Syntax

If str is the given string, then the syntax of the statement to remove the first character is

str = str.drop(1)
ADVERTISEMENT

Example

In the following program, we take a string in str, remove the first character from the string, and print the output.

Main.kt

fun main() {
    var str = "helloworld"
    str = str.drop(1)
    println(str)
}

Output

elloworld

Explanation

h e l l o w o r l d
0 1 2 3 4 5 6 7 8 9
h                   <- remove first char
  e l l o w o r l d <- resulting string

Conclusion

In this Kotlin Tutorial, we learned how to remove first character in a string using String.drop() function.