Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala string.split does not work

Following is my REPL output. I am not sure why string.split does not work here.

val s = "Pedro|groceries|apple|1.42" s: java.lang.String = Pedro|groceries|apple|1.42  scala> s.split("|") res27: Array[java.lang.String] = Array("", P, e, d, r, o, |, g, r, o, c, e, r, i, e, s, |, a, p, p, l, e, |, 1, ., 4, 2) 
like image 226
hrishikeshp19 Avatar asked Jul 01 '12 18:07

hrishikeshp19


People also ask

How to split the string in Scala?

Use the split() Method to Split a String in Scala Scala provides a method called split() , which is used to split a given string into an array of strings using the delimiter passed as a parameter. This is optional, but we can also limit the total number of elements of the resultant array using the limit parameter.

How does split work in Scala?

Split is used to divide the string into several parts containing inside an array because this function return us array as result. We can also limit our array elements by using the 'limit' parameter. Otherwise, it will just contain all the parameters from the string that has been spitted.


2 Answers

If you use quotes, you're asking for a regular expression split. | is the "or" character, so your regex matches nothing or nothing. So everything is split.

If you use split('|') or split("""\|""") you should get what you want.

like image 55
Rex Kerr Avatar answered Oct 13 '22 21:10

Rex Kerr


| is a special regular expression character which is used as a logical operator for OR operations.

Since java.lang.String#split(String regex); takes in a regular expression, you're splitting the string with "none OR none", which is a whole another speciality about regular expression splitting, where none essentially means "between every single character".

To get what you want, you need to escape your regex pattern properly. To escape the pattern, you need to prepend the character with \ and since \ is a special String character (think \t and \r for example), you need to actually double escape so that you'll end up with s.split("\\|").

For full Java regular expression syntax, see java.util.regex.Pattern javadoc.

like image 21
Esko Avatar answered Oct 13 '22 22:10

Esko