1. Overview

String is one of the most commonly used data types in Kotlin. Therefore, manipulating strings is a fundamental operation in our daily programming.

In this quick tutorial, we’ll explore various approaches to removing the first character from a string in Kotlin.

2. Introduction to the Problem

Strings are immutable in Java and Kotlin. Therefore, we cannot in-place remove characters from a string object. In other words**, we aim to obtain a new string object to hold the required result when we discuss removing the first character from a string.**

In this tutorial, we’ll address different solutions to do the job. Furthermore, we’ll discuss these solutions’ behavior in a corner case: when the string is empty.

For simplicity, we’ll use unit test assertions to verify whether each approach works as expected.

3. Using the drop() Function

Kotlin offers a series of convenient String extensions. For example, the drop(n) function can remove the first n characters from a string.

Therefore, if we only want to remove the first character, we can pass 1 to drop():

val myStr = "0a b c d e"
val result = myStr.drop(1)
assertEquals("a b c d e", result)

It’s worth noting that the string.drop(1) approach works with empty strings as well:

val result2 = "".drop(1)
assertEquals("", result2)

4. Using the substring() Function

If we’ve removed the first character from a string, the result is the same as we get the substring of the input from the second character. Therefore, we can also leverage the substring() method to solve the problem:

val myStr = "0a b c d e"
val result = myStr.substring(1)
assertEquals("a b c d e", result)

As the test above shows, we passed 1 to substring() since it’s the second character’s index.

However, this approach throws IndexOutOfBoundsException if the input string is empty:

//throw exception when the string is empty
assertThrows<IndexOutOfBoundsException> {
    "".substring(1)
}

5. Using the takeLast() Function

Similar to substring(), Kotlin’s takeLast() function returns a substring of the input. However, takeLast(n) takes the last n characters from the input string. For example, “123”.takeLast(2) returns “23”. Therefore, if we want to exclude the first character, takeLast(input.length – 1) solves the problem:

val myStr = "0a b c d e"
val result = myStr.takeLast(myStr.length - 1) // or takeLast(myStr.lastIndex)
assertEquals("a b c d e", result)

Alternatively, we can pass myStr.lastIndex to the takeLast() function since string.lastIndex has the same value as string.length – 1:

public val CharSequence.lastIndex: Int
    get() = this.length - 1

But this approach raises IllegalArgumentException if the string is empty:

val myEmpty = ""

assertThrows<IllegalArgumentException> {
    myEmpty.takeLast(myEmpty.length - 1)
}.also {
    assertEquals("Requested character count -1 is less than zero.", it.message)
}

6. Using the replace() Function

Another technique to remove characters from a string is the regex-based approach. For example, we can use the replace() function to solve the problem:

val myStr = "0a b c d e"
val result = myStr.replace("^.".toRegex(), "")
assertEquals("a b c d e", result)

The regex pattern “^.” matches the first character, so we use replace() to replace the first character with an empty string.

As the regex-based solution isn’t dependent on the character’s index, it works even if the string is empty:

val result2 = "".replace("^.".toRegex(), "")
assertEquals("", result2)

7. Conclusion

In this article, we explored several methods to remove the first character from a string in Kotlin. Also, we discussed how these solutions behave if the input string is empty.

As always, the complete source code for the examples is available over on GitHub.