Reputation: 1690
I use split in Java like this
String str = "Bx=1946^Cx=1043423";
String[] parts = str.split("^");
for (String part : parts)
System.out.println(part);
But it turns out there is only one element in parts array after splitting, so if I want to use "^" as delimiter, what should I write?
Thanks
Upvotes: 1
Views: 217
Reputation: 2333
Use
str.split(Pattern.quote("^"));
instead of
str.split("^");
Pattern.quote()
works in case of splitting with multiple symbols too. For example, we can use str.split(Pattern.quote("^^^^"));
instead of adding too many special characters like str.split("\\^\\^\\^\\^")
.
Upvotes: 2
Reputation: 7197
You need to escape the^
, which has a special meaning in regex (start of word, line and others):
String str = "Bx=1946^Cx=1043423";
String[] parts = str.split("\\^");
for (String part : parts)
System.out.println(part);
The output will be:
Bx=1946
Cx=1043423
Upvotes: 9