01: package ch.ethz.ssh2.util;
02:
03: /**
04: * Tokenizer. Why? Because StringTokenizer is not available in J2ME.
05: *
06: * @author Christian Plattner, plattner@inf.ethz.ch
07: * @version $Id: Tokenizer.java,v 1.1 2005/12/05 17:13:27 cplattne Exp $
08: */
09: public class Tokenizer {
10: /**
11: * Exists because StringTokenizer is not available in J2ME.
12: * Returns an array with at least 1 entry.
13: *
14: * @param source must be non-null
15: * @param delimiter
16: * @return an array of Strings
17: */
18: public static String[] parseTokens(String source, char delimiter) {
19: int numtoken = 1;
20:
21: for (int i = 0; i < source.length(); i++) {
22: if (source.charAt(i) == delimiter)
23: numtoken++;
24: }
25:
26: String list[] = new String[numtoken];
27: int nextfield = 0;
28:
29: for (int i = 0; i < numtoken; i++) {
30: if (nextfield >= source.length()) {
31: list[i] = "";
32: } else {
33: int idx = source.indexOf(delimiter, nextfield);
34: if (idx == -1)
35: idx = source.length();
36: list[i] = source.substring(nextfield, idx);
37: nextfield = idx + 1;
38: }
39: }
40:
41: return list;
42: }
43: }
|