Scala: Splitting with Double Quotes (“”) vs Single Quotes ('')

前端 未结 4 635
礼貌的吻别
礼貌的吻别 2020-12-20 03:01

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         


        
4条回答
  •  庸人自扰
    2020-12-20 03:36

    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 \|.

提交回复
热议问题