Java: split() method with pipe special character

前端 未结 7 1390
悲&欢浪女
悲&欢浪女 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:43

    Pipe is special regex symbol which means OR, if you want to split by pipe then escape it in your regex:

    String[] strbuf = str.split("\\|");
    

    OR

    String[] strbuf = str.split("[|]");
    

提交回复
热议问题