W3cubDocs

/Kotlin

dropLast

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun CharSequence.dropLast(n: Int): CharSequence

Returns a subsequence of this char sequence with the last n characters removed.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val string = "<<<First Grade>>>"
println(string.drop(6)) // st Grade>>>
println(string.dropLast(6)) // <<<First Gr
println(string.dropWhile { !it.isLetter() }) // First Grade>>>
println(string.dropLastWhile { !it.isLetter() }) // <<<First Grade
//sampleEnd
}

Exceptions

IllegalArgumentException - if n is negative.

Platform and version requirements: JVM (1.0), JS (1.0), Native (1.0)
fun String.dropLast(n: Int): String

Returns a string with the last n characters removed.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val string = "<<<First Grade>>>"
println(string.drop(6)) // st Grade>>>
println(string.dropLast(6)) // <<<First Gr
println(string.dropWhile { !it.isLetter() }) // First Grade>>>
println(string.dropLastWhile { !it.isLetter() }) // <<<First Grade
//sampleEnd
}

Exceptions

IllegalArgumentException - if n is negative.

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/drop-last.html