Way back when Java first came out, if you wanted to split a string into tokens, you had to roll your own mechanism to do so. Of course, even as far back as Java 1.2, there were some built-in secrets to help you tokenize your string so you could iterate over the tokens.

David S. found this little gem written by one of his cohorts in a very recent version of Java (which we all know has absolutely no way of splitting a string into tokens).

While it's plausible that someone new to Java might not know about the built-in function to tokenize a string, it's pretty clear from this piece of ingenuity that this individual also didn't seem to know about ArrayLists, or even System.arraycopy()...

public static String[] split(String toSplit, String delimiter) {
  String[] ret = new String[0];

  int i = toSplit.indexOf(delimiter);

  String[] temp = null;
  while(i>-1) {
    temp = new String[ret.length+1];
    for (int j=0; j < ret.length; j++) {
        temp[j] = ret[j];
    }
                     
    temp[temp.length-1] = toSplit.substring(0, i);
    toSplit =  toSplit.substring(i+ delimiter.length());
    i = toSplit.indexOf(delimiter);
                     
    ret = new String[temp.length];
    for (int j=0; j < ret.length; j++) {
        ret[j] = temp[j];
    }                    
  }
              
  temp = new String[ret.length+1];
  for (int j=0; j < ret.length; j++) {
      temp[j] = ret[j];
  }
              
  temp[temp.length-1] = toSplit;
              
  ret = new String[temp.length];
  for (int j=0; j < ret.length; j++) {
      ret[j] = temp[j];
  }             
              
  return ret;
}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!