Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use String.split on string with ^ (caret) symbol

I have a string 218~2~4~6^219~1~3~3^218~5~2~2^217~10~3~8^, I want to split the string by using character with ^ . I have tried like this

String mainString = "218~2~4~6^219~1~3~3^218~5~2~2^217~10~3~8^";

String[] tokens = mainString.split("^");

for (String stri: tokens){
        System.out.println("\nString tokens: " + stri);
    }

But It didn't work.. Please help me to split the string

like image 737
TONY Avatar asked Jan 29 '26 18:01

TONY


1 Answers

Use backslash(\) in front of ^ since its an special character(matches the beginning of the string) in regular expression. Once you add backslash(\) in the front, its treats ^ as a literal, which you require to perform the split.

 String[] tokens = mainString.split("\\^");
like image 107
Yogendra Singh Avatar answered Feb 01 '26 06:02

Yogendra Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!