scala string.split does not work

后端 未结 3 1077
孤独总比滥情好
孤独总比滥情好 2020-11-30 03:39

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|         


        
相关标签:
3条回答
  • 2020-11-30 04:26

    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.

    0 讨论(0)
  • 2020-11-30 04:35

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

    0 讨论(0)
  • 2020-11-30 04:39

    Split takes a regex as first argument, so your call is interpreted as "empty string or empty string". To get the expected behavior you need to escape the pipe character "\\|".

    0 讨论(0)
提交回复
热议问题