How to find a String between two delimiters in Java? [closed]

You can do this:

String [] tokens = str.replaceAll(";\\w*:?",":").split(":");

The first part removes the words you don’t need (Two, Four…) the second part splits the rest. Note that the first item in the result (tokes[0]] is empty as per your specification.

Example:

public static void main(String[] args) {
    String str = ":One;Two:Three;Four:Five;Six:Seven;";
    String [] tokens = str.replaceAll(";\\w*:?",":").split(":");
    for (int i = 0; i < tokens.length; i++) {
        System.out.println("token[" + i + "] = " + tokens[i]);
    }
}

prints

token[0] = 
token[1] = One
token[2] = Three
token[3] = Five
token[4] = Seven

Browse More Popular Posts

Leave a Comment