Shankar
Shankar

Reputation: 8957

String.split not working with combination of delimiter {^

I am trying to split the string with combination of {^

How to use combination of delimiter for splitting the string.

The sample data is :

String str = "0002{^000000000000001157{^000006206210015461{^PR{^ID{^62499{^";

Upvotes: 0

Views: 1193

Answers (3)

nafas
nafas

Reputation: 5423

split method in java takes an regex as an input.

so if you want to split the string using '{' and '^' then you need to do the following:

String str = "0002{^000000000000001157{^000006206210015461{^PR{^ID{^62499{^";

String[] splitted = str.split("\\{\\^");  //note \\ before { and ^

Upvotes: 3

Jens
Jens

Reputation: 69440

You have to escape { and ^ in your split Statement, because both are Special character in regex:

s.split("\\{\\^");

Upvotes: 2

fps
fps

Reputation: 34460

The delimiter passed to String.split() is a regex. As { and ^ are characters with special meaning within a regex, you need to escape them if you want to use them as literals:

String[] tokens = str.split("\\{\\^");

Upvotes: 4

Related Questions