blob: 6adea2438b196ece1944eee8981cd0878eaaeb73 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package org.the_jk.cleversync
object StringUtils {
fun split(input: String, delimiter: Char, keepEmpty: Boolean = true, limit: Int = 0): List<String> {
return buildList {
var offset = 0
var count = 0
while (true) {
val next = input.indexOf(delimiter, offset)
if (next == -1) {
if (keepEmpty || offset < input.length) {
if (limit > 0 && count == limit) {
add("${removeLast()}${delimiter}${input.substring(offset)}")
break
}
add(input.substring(offset))
}
break
}
if (keepEmpty || offset < next) {
if (limit > 0 && count == limit) {
add("${removeLast()}${delimiter}${input.substring(offset)}")
break
}
add(input.substring(offset, next))
count++
}
offset = next + 1
}
}
}
}
|