I want to split I_have_a_string into I_have_a and string.
Is there a built in function to split from end in Kotlin? The following is what I am doing now:
val words = myString.split("_")
val first = words.dropLast(1).joinToString("_")
val second = words.last()
substringBeforeLast() and substringAfterLast() are Kotlin built in functions very convenient for this case:
"I_have_a_string".substringBeforeLast("_") // output: "I_have_a"
"I_have_a_string".substringAfterLast("_") // output: "string"
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