Hi how to get last two word for string. if i have ...
# android
v
Hi how to get last two word for string. if i have sentence like this
Hey! well done men
. How can i get substring
done men
in kotlin. Anyone knows the easiest way
z
Copy code
string.split("\\s+".toRegex()).takeLast(2)
v
it converts List<String>
i need in single text @Zach Klippenstein (he/him) [MOD]
thanks for your help
z
you can find the position of the second-last whitespace group, get a substring from there. Using either Kotlin’s
Regex
API or java’s
Pattern
API, they’re similar
v
okk get it thanks a millon men
r
Copy code
.joinToString(" ")
you can just use the
joinToString
to join the list given in zach's answer to a String
v
thanks @Raed Ghazal i got it
👍 1