There are not so many problems with efficiency, everything that I see is linear.
In either case, you can use a regex or a manual tokenizer.
You can avoid the list. You know the length of the values and actions , so you can do
String[] values = s.split(";"); String[] actions = values[0].split(":"); String[] result = new String[actions.length + values.length - 1]; System.arraycopy(actions, 0, result, 0, actions.legnth); System.arraycopy(values, 1, result, actions.length, values.length - 1); return result;
It should be effective enough if you do not insist on implementing split yourself.
Unconfirmed approach at a low level (required before unit test and benchmark before use):
// Separator characters, as char, not string. final static int s1 = ':'; final static int s2 = ';'; // Compute required size: int components = 1; for(int p = Math.min(s.indexOf(s1), s.indexOf(s2)); p < s.length() && p > -1; p = s.indexOf(s2, p+1)) { components++; } String[] result = new String[components]; // Build result int in=0, i=0, out=Math.min(s.indexOf(s1), s.indexOf(s2)); while(out < s.length() && out > -1) { result[i] = s.substring(in, out); i++; in = out + 1; out = s.indexOf(s2, in); } assert(i == result.length - 1); result[i] = s.substring(in, s.length()); return result;
Note: this code is optimized in a crazy way that it will consider : only in the first component. Processing the last component is a bit complicated since out will have a value of -1 .
I would usually not use this latter approach if performance and memory are not extremely important. Most likely, it still has some errors, and the code is quite unreadable, especially in comparison with the above.