5

In the following Scala code I attempt to convert from a String that contains elements separated by "|" to a sequence Seq[String]. However the result is a WrappedArray of characters. How to make this work?

val array = "t1|t2".split("|")
println(array.toSeq)

results in:

WrappedArray(t, 1, |, t, 2)

What I need is:

Seq(t1,t2)
0

1 Answer 1

15

The below works. ie split by pipe character ('|') instead of pipe string ("|"). since split("|") calls overloaded definition that takes an regex string where pipe is a meta-character. This gets you the incorrect result as shown in the question.

scala> "t1|t2".split('|').toSeq
res10: Seq[String] = WrappedArray(t1, t2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.