Java split on ^ (caret?) not working, is this a special character?

前端 未结 5 1637
醉酒成梦
醉酒成梦 2020-11-30 13:33

In Java, I am trying to split on the ^ character, but it is failing to recognize it. Escaping \\^ throws code error.

Is this a special char

5条回答
  •  旧巷少年郎
    2020-11-30 14:00

    String.split() accepts a regex. The ^ sign is a special symbol denoting the beginning of the input sequence. You need to escape it to make it work. You were right trying to escape it with \ but it's a special character to escape things in Java strings so you need to escape the escape character with another \. It will give you:

    \\^
    

提交回复
热议问题