Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting from Array[String] to Seq[String] in Scala

Tags:

scala

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)
like image 313
ps0604 Avatar asked Feb 24 '17 03:02

ps0604


1 Answers

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)
like image 186
rogue-one Avatar answered Oct 04 '22 17:10

rogue-one