Tokenizer.java revision 48ded2421114c4c87ef3f8005c9f793a5d077cbd
1/*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5package ch.ethz.ssh2.util;
6
7/**
8 * Tokenizer. Why? Because StringTokenizer is not available in J2ME.
9 *
10 * @author Christian Plattner
11 * @version 2.50, 03/15/10
12 */
13public class Tokenizer
14{
15	/**
16	 * Exists because StringTokenizer is not available in J2ME.
17	 * Returns an array with at least 1 entry.
18	 *
19	 * @param source must be non-null
20	 * @param delimiter
21	 * @return an array of Strings
22	 */
23	public static String[] parseTokens(String source, char delimiter)
24	{
25		int numtoken = 1;
26
27		for (int i = 0; i < source.length(); i++)
28		{
29			if (source.charAt(i) == delimiter)
30				numtoken++;
31		}
32
33		String list[] = new String[numtoken];
34		int nextfield = 0;
35
36		for (int i = 0; i < numtoken; i++)
37		{
38			if (nextfield >= source.length())
39			{
40				list[i] = "";
41			}
42			else
43			{
44				int idx = source.indexOf(delimiter, nextfield);
45				if (idx == -1)
46					idx = source.length();
47				list[i] = source.substring(nextfield, idx);
48				nextfield = idx + 1;
49			}
50		}
51
52		return list;
53	}
54}
55