I have a String and i want to split it , and drop the last part .
For example something like that for this input :
var example = "Long string to split in the last space"
I want to achieve this result
var result = "Long string to split in the last"
Use substringBeforeLast
:
"Long string to split in the last space".substringBeforeLast(" ")
A more verbose alternative to substringBeforeLast that works for removing the last n
words by using dropLast:
var example = "Long string to split in the last space"
var result = example.split(" ")
.dropLast(1)
.joinToString(" ")
println(result) // Long string to split in the last
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With