Smallville7123
04/18/2019, 3:49 PMException in thread "main" java.lang.IndexOutOfBoundsException: start 4, end 3, s.length() 3
for https://pl.kotl.in/PEocYV7Ns fun String.padShrinkEnd(to: Int, char: Char): String {
val thisLength = this.length
val build = this.removeRange(to..thisLength).toStringBuilder()
while(build.length < thisLength) build.append(char)
return build.toString()
}
fun main() {
println("\"" + "abc".padShrinkEnd(2, ' ') + "\"")
}
streetsofboston
04/18/2019, 4:03 PMval build = this.removeRange(to..thisLength).toStringBuilder()
into
val build = this.removeRange(to until thisLength).toStringBuilder()
(replace ..
with `until`; was easy to debug, though…)streetsofboston
04/18/2019, 4:09 PMendIndex
of removeRange
is one past the position of the chars to be removed, according to the doc. The string’s length should be a valid value for `endIndex`….streetsofboston
04/18/2019, 4:13 PMendIndex
is inclusive for the range to remove… while the documentation says it is not inclusive….ilya.gorbunov
04/18/2019, 4:38 PMendIndex
parameter is exclusive, however if you make a range from it and pass that range to removeRange
the end index of that range is inclusive.ilya.gorbunov
04/18/2019, 4:40 PMthis.removeRange(to. thisLength)
is equivalent to this.substring(0, to)
or just this.take(to)
Smallville7123
04/18/2019, 5:20 PMthis.take
equivilant to this.dropLast
ilya.gorbunov
04/18/2019, 5:30 PMtake(n)
is equivalent to dropLast(size - n)
Smallville7123
04/18/2019, 5:39 PM