Java: split() method with pipe special character

前端 未结 7 1445
悲&欢浪女
悲&欢浪女 2021-01-24 16:14

I have a String = \"Hello-new-World\". And when i use the split() method with different regex values, it acts differently.

String str = \"Hello-new-world\"
Strin         


        
7条回答
  •  青春惊慌失措
    2021-01-24 16:44

    Presumably you're splitting on "|" in the second case - and | has a special meaning within regular expressions. If you want to split on the actual pipe character, you should escape it:

    String[] bits = whole.split(Pattern.quote("|"));
    

提交回复
热议问题