FileDocCategorySizeDatePackage
StrTokDemo4c.javaAPI DocExample1520Wed Oct 10 10:20:16 BST 2001None

StrTokDemo4c

public 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.

(Omit source code)

Fields Summary
public static final String
DELIM
The delimiter used between fields on input.
Constructors Summary
Methods Summary
public static voidmain(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 voidprintResults(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.Listprocess(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;