user3664724 user3664724 -3 years ago 81
Java Question

Split string on pattern coming first like ";;;" Explained in Post Java?

I want to break a string that has some unwanted characters initially and that has unlimited length.

For example:

;;;USA;23;john;;;USA:24;charles


The part I want is after the first ";;;", ie:

USA;23;john;;;USA:24;charles


I tried:

inputtring1 = ";;;USA;23;john;;;USA:24;charles";
String temp = inputString1.split(";;;")[1];


but it does not work; it shows result like

USA;23;john


";;;" may come in string in any number with other records. Above string is to make clear it's not in a fixed location.

Answer Source

If you just want everything after the first ";;;", just do this:

String stuff = str.replaceFirst(".*?;;;"), "");

A key point here is the reluctant quantifier *? which consumes as little as possible while still matching.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download