2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * The {@code StringTokenizer} class allows an application to break a string
22 * into tokens by performing code point comparison. The {@code StringTokenizer}
23 * methods do not distinguish among identifiers, numbers, and quoted strings,
24 * nor do they recognize and skip comments.
26 * The set of delimiters (the codepoints that separate tokens) may be specified
27 * either at creation time or on a per-token basis.
29 * An instance of {@code StringTokenizer} behaves in one of three ways,
30 * depending on whether it was created with the {@code returnDelimiters} flag
31 * having the value {@code true} or {@code false}:
33 * <li>If returnDelims is {@code false}, delimiter code points serve to separate
34 * tokens. A token is a maximal sequence of consecutive code points that are not
36 * <li>If returnDelims is {@code true}, delimiter code points are themselves
37 * considered to be tokens. In this case a token will be received for each
38 * delimiter code point.
41 * A token is thus either one delimiter code point, or a maximal sequence of
42 * consecutive code points that are not delimiters.
44 * A {@code StringTokenizer} object internally maintains a current position
45 * within the string to be tokenized. Some operations advance this current
46 * position past the code point processed.
48 * A token is returned by taking a substring of the string that was used to
49 * create the {@code StringTokenizer} object.
51 * Here's an example of the use of the default delimiter {@code StringTokenizer}
55 * StringTokenizer st = new StringTokenizer("this is a test");
56 * while (st.hasMoreTokens()) {
57 * println(st.nextToken());
63 * This prints the following output: <blockquote>
74 * Here's an example of how to use a {@code StringTokenizer} with a user
75 * specified delimiter: <blockquote>
78 * StringTokenizer st = new StringTokenizer(
79 * "this is a test with supplementary characters \ud800\ud800\udc00\udc00",
80 * " \ud800\udc00");
81 * while (st.hasMoreTokens()) {
82 * println(st.nextToken());
88 * This prints the following output: <blockquote>
104 public class StringTokenizer implements Enumeration<Object> {
106 private String string;
108 private String delimiters;
110 private boolean returnDelimiters;
112 private int position;
115 * Constructs a new {@code StringTokenizer} for the parameter string using
116 * whitespace as the delimiter. The {@code returnDelimiters} flag is set to
120 * the string to be tokenized.
122 public StringTokenizer(String string) {
123 this(string, " \t\n\r\f", false);
127 * Constructs a new {@code StringTokenizer} for the parameter string using
128 * the specified delimiters. The {@code returnDelimiters} flag is set to
129 * {@code false}. If {@code delimiters} is {@code null}, this constructor
130 * doesn't throw an {@code Exception}, but later calls to some methods might
131 * throw a {@code NullPointerException}.
134 * the string to be tokenized.
136 * the delimiters to use.
138 public StringTokenizer(String string, String delimiters) {
139 this(string, delimiters, false);
143 * Constructs a new {@code StringTokenizer} for the parameter string using
144 * the specified delimiters, returning the delimiters as tokens if the
145 * parameter {@code returnDelimiters} is {@code true}. If {@code delimiters}
146 * is null this constructor doesn't throw an {@code Exception}, but later
147 * calls to some methods might throw a {@code NullPointerException}.
150 * the string to be tokenized.
152 * the delimiters to use.
153 * @param returnDelimiters
154 * {@code true} to return each delimiter as a token.
156 public StringTokenizer(String string, String delimiters,
157 boolean returnDelimiters) {
158 if (string != null) {
159 this.string = string;
160 this.delimiters = delimiters;
161 this.returnDelimiters = returnDelimiters;
164 throw new NullPointerException();
168 * Returns the number of unprocessed tokens remaining in the string.
170 * @return number of tokens that can be retreived before an {@code
171 * Exception} will result from a call to {@code nextToken()}.
173 public int countTokens() {
175 boolean inToken = false;
176 for (int i = position, length = string.length(); i < length; i++) {
177 if (delimiters.indexOf(string.charAt(i), 0) >= 0) {
178 if (returnDelimiters)
194 * Returns {@code true} if unprocessed tokens remain. This method is
195 * implemented in order to satisfy the {@code Enumeration} interface.
197 * @return {@code true} if unprocessed tokens remain.
199 public boolean hasMoreElements() {
200 return hasMoreTokens();
204 * Returns {@code true} if unprocessed tokens remain.
206 * @return {@code true} if unprocessed tokens remain.
208 public boolean hasMoreTokens() {
209 if (delimiters == null) {
210 throw new NullPointerException();
212 int length = string.length();
213 if (position < length) {
214 if (returnDelimiters)
215 return true; // there is at least one character and even if
216 // it is a delimiter it is a token
218 // otherwise find a character which is not a delimiter
219 for (int i = position; i < length; i++)
220 if (delimiters.indexOf(string.charAt(i), 0) == -1)
227 * Returns the next token in the string as an {@code Object}. This method is
228 * implemented in order to satisfy the {@code Enumeration} interface.
230 * @return next token in the string as an {@code Object}
231 * @throws NoSuchElementException
232 * if no tokens remain.
234 public Object nextElement() {
239 * Returns the next token in the string as a {@code String}.
241 * @return next token in the string as a {@code String}.
242 * @throws NoSuchElementException
243 * if no tokens remain.
245 public String nextToken() {
246 if (delimiters == null) {
247 throw new NullPointerException();
250 int length = string.length();
253 if (returnDelimiters) {
254 if (delimiters.indexOf(string.charAt(position), 0) >= 0)
255 return String.valueOf(string.charAt(position++));
256 for (position++; position < length; position++)
257 if (delimiters.indexOf(string.charAt(position), 0) >= 0)
258 return string.substring(i, position);
259 return string.substring(i);
262 while (i < length && delimiters.indexOf(string.charAt(i), 0) >= 0)
266 for (position++; position < length; position++)
267 if (delimiters.indexOf(string.charAt(position), 0) >= 0)
268 return string.substring(i, position);
269 return string.substring(i);
272 throw new NoSuchElementException();
276 * Returns the next token in the string as a {@code String}. The delimiters
277 * used are changed to the specified delimiters.
280 * the new delimiters to use.
281 * @return next token in the string as a {@code String}.
282 * @throws NoSuchElementException
283 * if no tokens remain.
285 public String nextToken(String delims) {
286 this.delimiters = delims;