norman784
07/14/2017, 7:31 PMfun String.consumeWhile(test: (String) -> Boolean): String {
if (isEmpty()) return ""
val chars = toCharArray().toMutableList()
var result = ""
var i = -1
while (chars.isNotEmpty() && test(chars.first().toString())) {
result += chars.removeAt(0)
++i
}
removeRange(0..i) // this line should remove the consumed characters, but seems that is not working as I expect
return result
}
the usage should be
val myString = "--test"
myString.consumeWhile({ it != "-" })
print(myString) // output: "--test" but I expect the output to be just "test"