This article explores different ways to remove characters from the start of a string in Kotlin.

Since strings are immutable in Kotlin, we can’t remove characters from it. However, we can create a new string with the first n characters removed. This can be done using the substring() function with the starting index n, which creates a substring starting from position n till its end.

Download Code

 
If the index is negative or larger than the length of the string, the string index out of range exception is thrown. This can be handled by placing the range check before the substring() function.

Download Code

 
It is often required to pop a character from the beginning of the string if and only if it matches with the specific character. This can be implemented as follows in Kotlin.

Download Code

That’s all about removing characters from the start of a string in Kotlin.