Jan Bodnar
Jan Bodnar

Reputation: 11637

Kotlin regex split - removing trailing space

In Kotlin, I split a sentence into words. The split method adds an empty space a the end of the list, however. How to get rid of the trailing space? Is there a simple way to do it without creating a new list?

package com.zetcode

fun main(args: Array<String>) {

    val text = "I saw a fox in the wood. The fox had red fur."

    val pattern = "\\W+".toRegex()

    val words = pattern.split(text)

    println(words)
}

The example prints [I, saw, a, fox, in, the, wood, The, fox, had, red, fur, ]

Upvotes: 4

Views: 2974

Answers (2)

user8959091
user8959091

Reputation:

You can remove the last item:

val words = pattern.split(text).dropLastWhile { it == "" }

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626853

The empty item appears there because your string has a non-word . char at the end. You may just omit empty items to solve the problem:

val text = "I saw a fox in the wood. The fox had red fur."
val pattern = """\W+""".toRegex()
val words = pattern.split(text).filter { it.isNotBlank() }
println(words) // => [I, saw, a, fox, in, the, wood, The, fox, had, red, fur]

Alternatively, use a matching approach with the opposite pattern, \w+:

val pattern = """\w+""".toRegex()
val words = pattern.findAll(text).map{it.value}.toList()
// => [I, saw, a, fox, in, the, wood, The, fox, had, red, fur]

Upvotes: 4

Related Questions