I get string: `name: tom, weight: 50, age: 21` Ho...
# getting-started
i
I get string:
name: tom, weight: 50, age: 21
How I can get value
name
,
weight
,
age
?
a
Copy code
"name: tom, weight: 50, age: 21".split(",").map {
    it.split(":").first()
}.joinToString(",")
👌 1
i
Thank you but I maybe wrote incomprehensibly. I want to get value of
name
,
weight
,
age.
a
Copy code
"name: tom, weight: 50, age: 21".split(",").map {
    it.split(":")[1]
}.joinToString(",")
i
Thanks a lot!
One more question. How I can assign those values to variables?
g
Copy code
val (name, weight, age) = "name: tom, weight: 50, age: 21".split(",").map {
    it.split(":")[1]
}
or just do: val name = result[0] val weight = result[1]
i
what is the
result
?
a
Copy code
val result = "name: tom, weight: 50, age: 21".split(",").map {
    it.split(":")[1]
}
i
Thanks!
m
I prefer regexes for these kinds of things, try to learn up on those! 🙂👍
g
Why use regex if it can be easily parsed manually (and as a result you can implement a lot better error parsing than just generic success/fail as in case of regex)
s
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
🤣 3
m
didn't expect so much regex hate 😛
😄 2
a
@Sam That’s one of Jamie’s best quotes, and it is still true!
g
It's not a matter of hate, regex have their own usages, but it doesn't mean that they are the best solution in all situations when you need parsing