Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala String split bizareness

I wrote this code

val line = "Aaa Bbb Ccc"
line.split(" ")

which produces the following output as expected:

res31: Array[String] = Array(Aaa, Bbb, Ccc)

I change the code slightly:

val line = "Aaa|Bbb|Ccc"
line.split("|")

And now I don't understand the output:

res30: Array[String] = Array("", A, a, a, |, B, b, b, |, C, c, c)

Why did this happen?

like image 629
Knows Not Much Avatar asked Dec 01 '22 18:12

Knows Not Much


2 Answers

split takes a string representing the regex to split on - "|" is a regex for the empty string or another empty string, so it splits between every character. You need to escape the |:

line.split("\\|")

alternatively you can use the overload which takes a Char parameter to split on (defined in StringOps):

line.split('|')
like image 99
Lee Avatar answered Dec 04 '22 16:12

Lee


Pipe "|" is a regex character that means either of two options. In that case either empty or empty.

Try escaping it to use it as a character:

val line = "Aaa|Bbb|Ccc"
line.split("\\|")

res0: Array[String] = Array(Aaa, Bbb, Ccc)
like image 27
maasg Avatar answered Dec 04 '22 16:12

maasg