David Pesetsky
David Pesetsky

Reputation: 294

Kotlin: Split string whitespace delimiters

In Kotlin, I need to split a line by white spaces. I tried here using what I think is a space and a tab. Also in case there are multiple delimiters, I used a +. I try to grab the 3rd thing in that delimited string below:

val lines = File(MyFilePath).readLines()

val two = lines[7].trim().split("\\\s+","\\\t+")[2]

Upvotes: 7

Views: 8503

Answers (2)

Lucky Payal
Lucky Payal

Reputation: 39

Or you can simply use this :

Var arr : Array<String> = yourString.split(" ").toTypedArray()

Inside the split method just enter a white space and it will split your string.

Upvotes: -1

alexrnov
alexrnov

Reputation: 2534

Maybe try like this:

val list: List<String> = lines[7].trim().split("\\s+".toRegex())
val two = list[1]

Upvotes: 14

Related Questions