Scala Pattern Syntax Exception

后端 未结 3 2005
遥遥无期
遥遥无期 2021-01-27 09:55

I\'m trying to split a string in by the characters \"}{\". However I am getting an error:

> val string = \"{one}{two}\".split(\"}{\")
java.util.r         


        
3条回答
  •  野性不改
    2021-01-27 10:23

    Well... the reason is that split treats its parameter string as a regular expression.

    Now, both { and } are special character in regular expressions.

    So you will have to skip the special characters of regex world for split's argument, like this,

    val string = "{one}{two}".split("\\}\\{")
    // string: Array[String] = Array({one, two})
    

提交回复
热议问题