Ben Schmidt Ben Schmidt - 1 year ago 134
Scala Question

scala - one line convert string split to vals

I saw that following answer: Scala split string to tuple, but in the question the OP is asking for a string to a List. I would like to take a string, split it by some character, and convert it to a tuple so they can be saved as vals:

val (a,b,c) = "A.B.C".split(".").<toTupleMagic>

Is this possible? This would be a conversion from an
to a

Answer Source

It is unnecessary:

val Array(a, b, c) = "A.B.C".split('.')

Note that I converted the parameter to split from String to Char: if you pass a String, it is treated as a regex pattern, and . matches anything (so you'll get an array of empty strings back).

If you truly want to convert it to tuple, you can use Shapeless.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download