/** * Splits a string into tokens, separated by the given delimiter. The * delimiter is not included in the resulting list, nor is it part of any of * the resulting tokens. * * @param input * The string to split. * @param delimiter * The character used to separate tokens in the input string. * @param wantEmptyTokens * <code>true</code> if consecutive delimiters must generate * empty tokens; <code>false</code> to output only tokens of * length 1 or more. * @return List of tokens found within the input string. */ public static List<String> split(final String input, final char delimiter, final boolean wantEmptyTokens) { final ArrayList<String> result = new ArrayList<String>(); int curChar = 0; int nextChar = input.indexOf(delimiter, curChar); while (nextChar != -1) { if (input.charAt(curChar) != delimiter) result.add(input.substring(curChar, nextChar)); else if (wantEmptyTokens) result.add(""); curChar = nextChar; curChar++; nextChar = input.indexOf(delimiter, curChar); } if (curChar == input.length()) { // A delimiter ends the input. if (wantEmptyTokens) result.add(""); } else // Add the last token. result.add(input.substring(curChar)); return result; }