Revan
Revan

Reputation: 261

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 Array[String] to a Tuple3 of (String,String,String)

Upvotes: 1

Views: 789

Answers (1)

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297195

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.

Upvotes: 4

Related Questions