Skip to content

Instantly share code, notes, and snippets.

@Robijnvogel
Created December 6, 2017 01:25
Show Gist options
  • Save Robijnvogel/4fdbfecb4653da48d05ae1b794dac5f1 to your computer and use it in GitHub Desktop.
Save Robijnvogel/4fdbfecb4653da48d05ae1b794dac5f1 to your computer and use it in GitHub Desktop.
private static String[] explodeString(String input, char[] delimiters) { //this HAS to be an array, I think. I have no idea how one would pass an unknown number of arguments INTO a method call
if (input.length() <= 0) {
return new String[0]; //because we are not interested in empty Strings between two delimiters, are we?
} else if (delimiters.length <= 0) {
return new String[]{input};
}
int delimPointer = -1;
List<Character> nextDelims = new ArrayList();
for (char d : delimiters) {
if (delimPointer >= 0) { //after first valid delimiter has been found
nextDelims.add(d); //just pass the delimiter to the next iteration
} else { //before first valid delimiter has been found
for (int i = 0; i < input.length(); i++) { //todo go from middle to edges instead of reading from left to right (or read from right to left
if (input.charAt(i) == d) {
while (input.charAt(input.length() - 1) == d) {
input = input.substring(0, input.length() - 1);// remove trailing delimiters
}
while (input.charAt(0) == d) {
input = input.substring(1, input.length());// remove leading delimiters
i--; //to keep the pointer pointed at the same character
}
if (input.length() <= 0) {
return new String[0]; //because we are not interested in empty Strings between two delimiters, are we?
}
if (i < 0 || i >= input.length()) { // if this instance of this delimiter has been removed as a result of removing the trailing or leading delimiters
i = 0; //start the for-loop over from the new beginning of input
} else { //this instance of the delimiter is guaranteedly not the first or the last character in input
delimPointer = i; // pick the position of this delimiter as the splitting point for recursion
nextDelims.add(d); //the delimiter can be possibly found again, so pass it on to the next iteration
break;
}
}
//if this line is reached, this delimiter has not been found thus far, except perhaps leading or trailing, so don't pass it on to the next generation
}
}
}
if (delimPointer < 0) { //none of the delimiters were found in input except perhaps leading or trailing
return new String[]{input};
} else {
char[] nextDelimsAsArray = new char[nextDelims.size()];
for (int i = 0; i < nextDelims.size(); i++) {
nextDelimsAsArray[i] = nextDelims.get(i);
}
//todo split the input on delimpointer
//todo recursively call this method and merge the results
//keep in mind that the return values can be String arrays of any size, even 0
}
return null;//TODO so the IDE will shut up
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment