// 7/28/03-7/29/03 package net.bunnie; public class Explode { public static String[] explode (String delimiter, String toExplode) { int howMany = 1; int howManyTwice = 0; int whereToStartAt = 0; int whereToEndAt = 0; String substr; String[] explodedArray; int isDelim = 0; // if delimiter is not more than 1 char long if (delimiter.length() <= 1) { // if one char long, do explodeChar if (delimiter.length() == 1) { char delim = delimiter.charAt(0); explodedArray = explodeChar(delim, toExplode); } // otherwise return as array[0] = string else { explodedArray = new String[1]; explodedArray[0] = "" + toExplode; } } // if delimiter more than 1 char else { for (int i = 0; i + delimiter.length() - 1 <= toExplode.length() - 1; i++) { substr = toExplode.substring(i, delimiter.length() + i); if (substr.equals(delimiter)) { howMany++; } } explodedArray = new String[howMany]; // make not-null values for (int i = 0; i < howMany; i++) { explodedArray[i] = ""; } if (howMany == 1) { explodedArray[0] = "" + toExplode; } else { for (int i = 0; i <= toExplode.length() - 1; i++) { if (i + delimiter.length() - 1 <= toExplode.length() - 1) { // if substr starting with char@i = to delim, make new element for array if (toExplode.substring(i, delimiter.length() + i).equals(delimiter)) { howManyTwice++; i += delimiter.length() - 1; } // otherwise add char to current array element else { explodedArray[howManyTwice] += toExplode.charAt(i); } } // once loop finishes, add rest of chars to last element else { explodedArray[howManyTwice] += toExplode.charAt(i); } } } } return explodedArray; } public static String[] explodeChar (char delimiter, String toExplode) { // explode a string by a character delimiter int howMany = 1; int howManyTwice = 0; String[] explodedArray; // count how many there are for array for (int i = 0; i <= toExplode.length() - 1; i++) { if (toExplode.charAt(i) == delimiter) { howMany++; } } explodedArray = new String[howMany]; // make not-null values for (int i = 0; i < howMany; i++) { explodedArray[i] = ""; } // if no delim, make array with string as array[0] if (howMany == 1) { explodedArray[0] = "" + toExplode; } // otherwise make array split by delim else { for (int i = 0; i <= toExplode.length() - 1; i++) { // if char = to delim, make new element for array if (toExplode.charAt(i) == delimiter) { howManyTwice++; } // otherwise add char to current array element else { explodedArray[howManyTwice] += toExplode.charAt(i); } } } return explodedArray; } }