Kotlin Split Sentence String Into Words

May 18, 2019
val text = "I'm home, baby 小米."
val words = text.split("\\s+".toRegex()).map { word ->    word.replace("""^[,\.]|[,\.]$""".toRegex(), "")}

Result

["I'm", "home", "baby", "小米"]

Option 2

val words = text.split("\\P{L}+".toRegex())

Result

["I", "m", "home", "baby", "小米", ""]

Option 3

val words = text.split("\\s+".toRegex())

Result

["I'm", "home", "", "baby", "小米."]

Option 4

val words = text.split("\\W+".toRegex())

Result

["I", "m", "home", "baby", ""]

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.