I am splitting a String in Scala with Comma using Double Quotes, like this:
scala> val a = \"a,b,c\"
a: String = a,b,c
scala> a.split(\",\")
res0: Arr
Use it as char
scala> a.split('|')
res5: Array[String] = Array(a, b, c)
It works, you need to use escape sequence. Pipe (|) has a special meaning. You need to use escape sequence to say its a normal char.
scala> val a = "a|b|c"
a: String = a|b|c
scala> a.split("\\|")
res2: Array[String] = Array(a, b, c)
with triple quotes
scala> a.split("""\|""")
res4: Array[String] = Array(a, b, c)
Pipe (|) is a metacharacter in regex. You'd need to escape it:
scala> a.split("\\|")
res1: Array[String] = Array(a, b, c)
First of all '|'
is Char
whereas "|"
is String
.
scala> val a1 = '|'
// a1: Char = |
scala> val a2 = "|"
// a2: String = |
Now, when you pass a String
to String.split
it uses it to create a a regular expression, which is then used to split the String
.
And |
is a special character in regular expressions.
Which means that you need to escape
your |
so that it will not be considered as a special character. In regular expressions, we use \
to mark the next character as escaped
.
scala> val a = "a|b|c"
// a: String = a|b|c
scala> a.split(raw"\|")
// res2: Array[String] = Array(a, b, c)
scala>a.split("\\|")
// res0: Array[String] = Array(a, b, c)
Or,
scala> val a = "a|b|c"
// a: String = a|b|c
scala> val regex = raw"\|".r
// regex: scala.util.matching.Regex = \|
scala> regex.split(a)
// res1: Array[String] = Array(a, b, c)
Notice that \\|
and \|
difference when not using raw
, that is because \
is a special character in Scala Strings
.
So when we are not using raw
String
we have to first tell that the next \
is not a special character and will not be processed by String
, hence left as it is. So the processed String
will look like \|
.
The split method is overloaded. If you use single quote '
you are splitting by a character:
def split(separator: Char)
If you use double quote "
you are using java split that takes a regex:
public String[] split(String regex)
Since pipe |
is a reserved character in a regex you need to escape it if you are using double quotes:
a.split("\\|")