How to split the string using '^' this special character in java?

后端 未结 3 936
北恋
北恋 2020-12-03 15:06

I want to split the following string \"Good^Evening\" i used split option it is not split the value. please help me.

This is what I\'ve been trying:



        
3条回答
  •  没有蜡笔的小新
    2020-12-03 15:11

    The regex you should use is "\^" which you write as "\\^" as a Java String literal; i.e.

    String[] parts = "Good^Evening".split("\\^");
    

    The regex needs a '\' escape because the caret character ('^') is a meta-character in the regex language. The 2nd '\' escape is needed because '\' is an escape in a String literal.

提交回复
热议问题