Didn't find it but is there a `substring` method :...
# stdlib
x
Didn't find it but is there a
substring
method :
Copy code
fun String.substring(maxLength : Int): String {
    return if (length > maxLength) substring(0, maxLength) else this
}
The goal here being to avoid the IndexOutOfBoundsException when the string is shorter than the desired length
d
String.substring(Int)
already means "substring with start index", so a different name would have to be chosen. However you can easily do this in a pretty expressive way already:
x.substring(0, min(x.length, 5))
k
I think you are looking for
take
?
K 1
7
Copy code
fun main() {
    println("abc".take(0))
    println("abc".take(2))
    println("abc".take(5))
}
prints
Copy code
ab
abc
💯 3