Published Dec 6, 2023
Contribute to Docs

The Kotlin method, .dropLast() removes one or n number of characters from the end of the string. Here, n is a positive integer given as a parameter to the method. This method does not modify the original list, it creates and returns a new list.


fun String.dropLast(n: Int): String
  • The parameter n is the number of characters to be removed from the string.

Note: IllegalArguementException error thrown if n is a negative number. A negative number of characters can not be removed.


The example below creates a string, string, then uses the .dropLast() method to return a string with the last five characters removed.

fun main() {
val string = "Example String"

The output will look like the following:

Example Strin
Example S

All contributors

Looking to contribute?

Learn Kotlin on Codecademy