Q:

The efficient way to split a String in Java

I have some inputs like this pattern:

org.apache.felix.dm;version="[4.1,5)",org.osgi.framework;version="[1.5,2)",org.osgi.service.cm;version="[1.3,2)",org.osgi.service.deploymentadmin;version="[1.0,2)",org.osgi.service.deploymentadmin.spi;version="[1.0,2)",org.osgi.service.event;version="[1.2,2)",org.osgi.service.log;version="[1.3,2)",org.osgi.service.metatype;version="[1.3,2)",org.osgi.util.tracker;version="[1.4,2)"

And I need to split it and store it in objects or list like this:

Object1:
String name = "org.apache.felix.dm";
String[] versions = {4.1.0, 5.0.0}

Object2:
String name = "org.osgi.framework";
String[] versions = {1.5.0, 2.0.0}

A:

I would solve this in 3 steps
Step 1:
split the text in pairs of name and versions. so you would end up with
org.apache.felix.dm;version="[4.1,5)"
org.osgi.framework;version="[1.5,2)"
org.osgi.service.cm;version="[1.3,2)"
org.osgi.service.deploymentadmin;version="[1.0,2)"
org.osgi.service.deploymentadmin.spi;version="[1.0,2)"
org.osgi.service.event;version="[1.2,2)"
org.osgi.service.log;version="[1.3,2)"
org.osgi.service.metatype;version="[1.3,2)"
org.osgi.util.tracker;version="[1.4,2)"

for this you can do the following:
String text = "org.apache.felix.dm;version=\"[4.1,5)\",org.osgi.framework;version=\"[1.5,2)\",org.osgi.service.cm;version=\"[1.3,2)\",org.osgi.service.deploymentadmin;version=\"[1.0,2)\",org.osgi.service.deploymentadmin.spi;version=\"[1.0,2)\",org.osgi.service.event;version=\"[1.2,2)\",org.osgi.service.log;version=\"[1.3,2)\",org.osgi.service.metatype;version=\"[1.3,2)\",org.osgi.util.tracker;version=\"[1.4,2)\"";
    String[] strArray = text.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");

strArray now contains the pairs.
doing
for (String string : strArray) {
    System.out.println(string);
}

prints 
org.apache.felix.dm;version="[4.1,5)"
org.osgi.framework;version="[1.5,2)"
org.osgi.service.cm;version="[1.3,2)"
org.osgi.service.deploymentadmin;version="[1.0,2)"
org.osgi.service.deploymentadmin.spi;version="[1.0,2)"
org.osgi.service.event;version="[1.2,2)"
org.osgi.service.log;version="[1.3,2)"
org.osgi.service.metatype;version="[1.3,2)"
org.osgi.util.tracker;version="[1.4,2)"

Step 2:
create an object for every entry within the strArray and store them in a array or list (im choosing ArrayList this time, you can chosse whatever you want tho):
    String text = "org.apache.felix.dm;version=\"[4.1,5)\",org.osgi.framework;version=\"[1.5,2)\",org.osgi.service.cm;version=\"[1.3,2)\",org.osgi.service.deploymentadmin;version=\"[1.0,2)\",org.osgi.service.deploymentadmin.spi;version=\"[1.0,2)\",org.osgi.service.event;version=\"[1.2,2)\",org.osgi.service.log;version=\"[1.3,2)\",org.osgi.service.metatype;version=\"[1.3,2)\",org.osgi.util.tracker;version=\"[1.4,2)\"";
    String[] strArray = text.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");  
    ArrayList<MyObject> list = new ArrayList<>();
    for (String string : strArray) {
        String currentPair[] = string.split(";");
        list.add(new MyObject(currentPair[0], currentPair[1]
                .replaceAll("\"", "")
                .replaceAll("version=", "")
                .replaceAll("\\[", "")
                .replaceAll("\\)", "").split(",")));
    }

    for (MyObject myObject : list) {
        System.out.print("Name: " + myObject.name + " - Versions:");
        for (String version : myObject.versions) {
            System.out.print(version + " ");
        }
        System.out.println();
    }

MyObject beeing your object's class.
prints the following:
Name: org.apache.felix.dm - Versions:4.1 5 
Name: org.osgi.framework - Versions:1.5 2 
Name: org.osgi.service.cm - Versions:1.3 2 
Name: org.osgi.service.deploymentadmin - Versions:1.0 2 
Name: org.osgi.service.deploymentadmin.spi - Versions:1.0 2 
Name: org.osgi.service.event - Versions:1.2 2 
Name: org.osgi.service.log - Versions:1.3 2 
Name: org.osgi.service.metatype - Versions:1.3 2 
Name: org.osgi.util.tracker - Versions:1.4 2 

now this is almost what you want, only thing left ist to format the version numbers to have 3 dots.
Step 3: format version string to have 3 dots:
for that i wrote a method and put the currentpair[1]... as a parameter
public static void main(String[] args) {
    String text = "org.apache.felix.dm;version=\"[4.1,5)\",org.osgi.framework;version=\"[1.5,2)\",org.osgi.service.cm;version=\"[1.3,2)\",org.osgi.service.deploymentadmin;version=\"[1.0,2)\",org.osgi.service.deploymentadmin.spi;version=\"[1.0,2)\",org.osgi.service.event;version=\"[1.2,2)\",org.osgi.service.log;version=\"[1.3,2)\",org.osgi.service.metatype;version=\"[1.3,2)\",org.osgi.util.tracker;version=\"[1.4,2)\"";
    String[] strArray = text.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    ArrayList<MyObject> list = new ArrayList<>();
    for (String string : strArray) {
        String currentPair[] = string.split(";");
        list.add(new MyObject(currentPair[0], addDots(currentPair[1]
                .replaceAll("\"", "")
                .replaceAll("version=", "")
                .replaceAll("\\[", "")
                .replaceAll("\\)", "").split(","), 3)));
    }

    for (MyObject myObject : list) {
        System.out.print("Name: " + myObject.name + " - Versions:");
        for (String version : myObject.versions) {
            System.out.print(version + " ");
        }
        System.out.println();
    }
}

public static String[] addDots(String toAdd[], int dotcount) {
    String[] formattedVersions = new String[toAdd.length];
    for (int i = 0; i < toAdd.length; i++) {
        ArrayList<String> digits = new ArrayList<>();
        digits.add(toAdd[i]);
        if(toAdd[i].contains(".")) {
            digits = new ArrayList<>(Arrays.asList(toAdd[i].split("\\.")));
        }
        for(int x = digits.size(); x < dotcount; x++) {
            digits.add("0");
        }
        String curretnFormattedVersion = "";
        for (int j = 0; j < digits.size() - 1; j++) {
            curretnFormattedVersion += digits.get(j) + ".";
        }
        curretnFormattedVersion += digits.get(dotcount - 1);
        formattedVersions[i] = curretnFormattedVersion;
    }
    return formattedVersions;
}

now this prints the following:
Name: org.apache.felix.dm - Versions: 4.1.0 5.0.0 
Name: org.osgi.framework - Versions: 1.5.0 2.0.0 
Name: org.osgi.service.cm - Versions: 1.3.0 2.0.0 
Name: org.osgi.service.deploymentadmin - Versions: 1.0.0 2.0.0 
Name: org.osgi.service.deploymentadmin.spi - Versions: 1.0.0 2.0.0 
Name: org.osgi.service.event - Versions: 1.2.0 2.0.0 
Name: org.osgi.service.log - Versions: 1.3.0 2.0.0 
Name: org.osgi.service.metatype - Versions: 1.3.0 2.0.0 
Name: org.osgi.util.tracker - Versions: 1.4.0 2.0.0 

which should be exactly what you need.
i cant tell wether or not this is efficient, but from the length of the code i would say no but hey, its working :D

