StrTokDemo4cpublic class StrTokDemo4c extends Object Show using a StringTokenizer including getting the delimiters back
This final version returns a List instead of converting to an array. |
Fields Summary |
---|
public static final String | DELIMThe delimiter used between fields on input. |
Methods Summary |
---|
public static void | main(java.lang.String[] a)
printResults("A|B|C|D", process("A|B|C|D"));
printResults("A||C|D", process("A||C|D"));
printResults("A|||D|E", process("A|||D|E"));
| public static void | printResults(java.lang.String input, java.util.List outputs)
System.out.println("Input: " + input);
Iterator it = outputs.iterator();
for (int i=0; it.hasNext(); i++)
System.out.println("Output " + i + " was: " + it.next());
| public static java.util.List | process(java.lang.String line)Processes one String, returns it as an array of fields
// A ArrayList is an array-like container that grows dynamically;
// see the chapter on Java Collections.
ArrayList results = new ArrayList();
// Unless you ask StringTokenizer to give you the tokens,
// it silently discards multiple null tokens.
StringTokenizer st = new StringTokenizer(line, DELIM, true);
int i = 0;
// Stuff each token into the Vector
while (st.hasMoreTokens()) {
String s = st.nextToken();
if (s.equals(DELIM)) {
// Just ignore the "tokens" that consist of DELIM
// But leave a blank entry corresponding to the missing field
if (results.size() < ++i)
results.add(null);
continue;
}
results.add(s);
}
return results;
|
|