0

How do I split a string into 4 array parts in kotlin?

I'm new to kotlin and I am trying to make a program that reads a .txt file which contains the following columns :name, surname, age, email, postcode. I want to read the text file and line by line and split each line into 4 parts. Name, surname, age and email. (Ignoring the postcode) Then store those in separate lists. I know how to do this in java but can't do it in kotlin. Sorry in advance, I'm very new to kotlin. The code I have used in Java is similar to this: String[] parts = string.split(" "); String part1 = parts[0]; String part2 = parts[1]; What would be the equivalent in kotlin? Thank you in advance.

5th Mar 2020, 2:37 AM
MBanski
MBanski - avatar
4 Respuestas
+ 6
You can also used: val (name, surname, age, email) = string.split(" ")
6th Mar 2020, 8:02 PM
John Wells
John Wells - avatar
+ 6
fun main(args: Array<String>) { val parts = "Wasd Ijkl Zzc Qwer".split(' ') val part1 = parts[0] val part2 = parts[1] println(parts) }
5th Mar 2020, 2:46 AM
Kevin ★
+ 4
You are welcome. Don't feel stupid. Stupid are those who give up. Keep learning and practicing. I am sure you will finish your program soon.
5th Mar 2020, 5:54 PM
Kevin ★
+ 1
Omg I feel stupid. Thank you!
5th Mar 2020, 1:03 PM
MBanski
MBanski - avatar