Xavier F. Gouchet
03/03/2020, 11:00 AMsubstring
method :
fun String.substring(maxLength : Int): String {
return if (length > maxLength) substring(0, maxLength) else this
}
Xavier F. Gouchet
03/03/2020, 11:00 AMdiesieben07
03/03/2020, 11:08 AMString.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))
Kristoffer Andersen
03/03/2020, 11:29 AMtake
?Kristoffer Andersen
03/03/2020, 11:33 AMfun main() {
println("abc".take(0))
println("abc".take(2))
println("abc".take(5))
}
prints
ab
abc