This class can split a String into many smaller strings. It aims to do a similar job to
StringTokenizer, however it offers much more control and flexibility including
implementing the ListIterator interface. By default, it is set up like StringTokenizer.
The input String is split into a number of tokens. Each token is separated from the next String by a delimiter. One or more delimiter characters must be specified.
Each token may be surrounded by quotes. The quote matcher specifies the quote character(s). A quote may be escaped within a quoted section by duplicating itself.
Between each token and the delimiter are potentially characters that need trimming. The trimmer matcher specifies these characters. One usage might be to trim whitespace characters.
At any point outside the quotes there might potentially be invalid characters. The ignored matcher specifies these characters to be removed. One usage might be to remove new line characters.
Empty tokens may be removed or returned as null.
"a,b,c" - Three tokens "a","b","c" (comma delimiter) " a, b , c " - Three tokens "a","b","c" (default CSV processing trims whitespace) "a, ", b ,", c" - Three tokens "a, " , " b ", ", c" (quoted text untouched)
| Property | Type | Default |
|---|---|---|
| delim | CharSetMatcher | { \t\n\r\f} |
| quote | NoneMatcher | {} |
| ignore | NoneMatcher | {} |
| emptyTokenAsNull | boolean | false |
| ignoreEmptyTokens | boolean | true |
| Modifier and Type | Field and Description |
|---|---|
| private char[] | chars
The text to work on. |
| private static final StringTokenizer | CSV_TOKENIZER_PROTOTYPE
Comma separated values tokenizer internal variable. |
| private StringMatcher | delimMatcher
The delimiter matcher. |
| private boolean | emptyAsNull
Whether to return empty tokens as null. |
| private StringMatcher | ignoredMatcher
The ignored matcher. |
| private boolean | ignoreEmptyTokens
Whether to ignore empty tokens. |
| private StringMatcher | quoteMatcher
The quote matcher. |
| private int | tokenPos
The current iteration position. |
| private String[] | tokens
The parsed tokens. |
| private StringMatcher | trimmerMatcher
The trimmer matcher. |
| private static final StringTokenizer | TSV_TOKENIZER_PROTOTYPE
Tab separated values tokenizer internal variable. |
| Access | Constructor and Description |
|---|---|
| public | StringTokenizer()
Constructs a tokenizer splitting on space, tab, newline and form feed as per StringTokenizer, but with no text to tokenize. |
| public | StringTokenizer(final char[]
the string which is to be parsed, not cloned. input)Constructs a tokenizer splitting on space, tab, newline and form feed as per StringTokenizer. |
| public | StringTokenizer(final char[]
the string which is to be parsed, not cloned. input, final char the field delimiter character. delim)Constructs a tokenizer splitting on the specified character. |
| public | StringTokenizer(final char[]
the string which is to be parsed, not cloned. input, final char the field delimiter character. delim, final char the field quoted string character. quote)Constructs a tokenizer splitting on the specified delimiter character and handling quotes using the specified quote character. |
| public | StringTokenizer(final char[]
the string which is to be parsed, not cloned. input, final String the field delimiter string. delim)Constructs a tokenizer splitting on the specified string. |
| public | StringTokenizer(final char[]
the string which is to be parsed, not cloned. input, final StringMatcher the field delimiter matcher. delim)Constructs a tokenizer splitting using the specified delimiter matcher. |
| public | StringTokenizer(final char[]
the string which is to be parsed, not cloned. input, final StringMatcher the field delimiter character. delim, final StringMatcher the field quoted string character. quote)Constructs a tokenizer splitting using the specified delimiter matcher and handling quotes using the specified quote matcher. |
| public | StringTokenizer(final String
the string which is to be parsed. input)Constructs a tokenizer splitting on space, tab, newline and form feed as per StringTokenizer. |
| public | StringTokenizer(final String
the string which is to be parsed. input, final char the field delimiter character. delim)Constructs a tokenizer splitting on the specified delimiter character. |
| public | StringTokenizer(final String
the string which is to be parsed. input, final char the field delimiter character. delim, final char the field quoted string character. quote)Constructs a tokenizer splitting on the specified delimiter character and handling quotes using the specified quote character. |
| public | StringTokenizer(final String
the string which is to be parsed. input, final String the field delimiter string. delim)Constructs a tokenizer splitting on the specified delimiter string. |
| public | StringTokenizer(final String
the string which is to be parsed. input, final StringMatcher the field delimiter matcher. delim)Constructs a tokenizer splitting using the specified delimiter matcher. |
| public | StringTokenizer(final String
the string which is to be parsed. input, final StringMatcher the field delimiter matcher. delim, final StringMatcher the field quoted string matcher. quote)Constructs a tokenizer splitting using the specified delimiter matcher and handling quotes using the specified quote matcher. |
| Modifier and Type | Method and Description |
|---|---|
| public void | add(final String
this parameter ignored. obj)Implements java. Unsupported ListIterator operation. |
| private void | |
| private void | |
| public Object | Returns: a new instance of this Tokenizer which has been reset.Overrides java. Creates a new instance of this Tokenizer. |
| pack-priv Object | Returns: a new instance of this Tokenizer which has been reset.Creates a new instance of this Tokenizer. |
| public String | Returns: The string content being parsed.Gets the String content that the tokenizer is parsing. |
| private static StringTokenizer | Returns: a clone ofCSV_TOKENIZER_PROTOTYPE.Returns a clone of |
| public static StringTokenizer | Returns: a new tokenizer instance which parses Comma Separated Value strings.Gets a new tokenizer instance which parses Comma Separated Value strings initializing it with the given input. |
| public static StringTokenizer | Returns: a new tokenizer instance which parses Comma Separated Value strings.the text to parse. input)Gets a new tokenizer instance which parses Comma Separated Value strings initializing it with the given input. |
| public static StringTokenizer | Returns: a new tokenizer instance which parses Comma Separated Value strings.the text to parse. input)Gets a new tokenizer instance which parses Comma Separated Value strings initializing it with the given input. |
| public StringMatcher | |
| public StringMatcher | |
| public StringMatcher | |
| public String[] | Returns: The tokens as a String array.Gets a copy of the full token list as an independent modifiable array. |
| public List | Returns: The tokens as a String list.Gets a copy of the full token list as an independent modifiable list. |
| public StringMatcher | |
| private static StringTokenizer | Returns: a clone ofTSV_TOKENIZER_PROTOTYPE.Returns a clone of |
| public static StringTokenizer | Returns: a new tokenizer instance which parses Tab Separated Value strings.Gets a new tokenizer instance which parses Tab Separated Value strings. |
| public static StringTokenizer | Returns: a new tokenizer instance which parses Tab Separated Value strings.the string to parse. input)Gets a new tokenizer instance which parses Tab Separated Value strings. |
| public static StringTokenizer | Returns: a new tokenizer instance which parses Tab Separated Value strings.the string to parse. input)Gets a new tokenizer instance which parses Tab Separated Value strings. |
| public boolean | Returns: true if there are more tokens.Implements java. Tests whether there are any more tokens. |
| public boolean | Returns: true if there are previous tokens.Implements java. Tests whether there are any previous tokens that can be iterated to. |
| public boolean | Returns: true if empty tokens are returned as null.Tests whether the tokenizer currently returns empty tokens as null. |
| public boolean | Returns: true if empty tokens are not returned.Tests whether the tokenizer currently ignores empty tokens. |
| private boolean | Returns: true if a quote is matched.the character array being tokenized. srcChars, final int the position to check for a quote. pos, final int the length of the character array being tokenized. len, final int the start position of the matched quote, 0 if no quoting. quoteStart, final int the length of the matched quote, 0 if no quoting. quoteLen)Tests if the characters at the index specified match the quote already matched in readNextToken(). |
| public String | |
| public int | Returns: The next token index.Implements java. Gets the index of the next token to return. |
| public String | Returns: The next sequential token, or null when no more tokens are found.Gets the next token from the String. |
| public String | Returns: The previous token.Implements java. Gets the token previous to the last returned token. |
| public int | Returns: The previous token index.Implements java. Gets the index of the previous token. |
| public String | Returns: The previous sequential token, or null when no more tokens are found.Gets the previous token from the String. |
| private int | Returns: The starting position of the next field (the character immediately after the delimiter), or -1 if end of string found.the character array being tokenized. srcChars, int the first character of field. start, final int the length of the character array being tokenized. len, final TextStringBuilder a temporary work area. workArea, final List<String> the list of parsed tokens. tokenList)
References Deprecated
TextStringBuilder is deprecated or references (maybe indirectly) at least one deprecated element.
Reads character by character through the String to get the next token. |
| private int | Returns: The starting position of the next field (the character immediately after the delimiter, or if end of string found, then the length of string.the character array being tokenized. srcChars, final int the first character of field. start, final int the length of the character array being tokenized. len, final TextStringBuilder a temporary work area. workArea, final List<String> the list of parsed tokens. tokenList, final int the start position of the matched quote, 0 if no quoting. quoteStart, final int the length of the matched quote, 0 if no quoting. quoteLen)
References Deprecated
TextStringBuilder is deprecated or references (maybe indirectly) at least one deprecated element.
Reads a possibly quoted string token. |
| public void | remove()
Implements java. Throws |
| public StringTokenizer | Returns: this instance.Resets this tokenizer, forgetting all parsing and iteration already completed. |
| public StringTokenizer | Returns: this instance.the new character array to tokenize, not cloned, null sets no text to parse. input)Resets this tokenizer, giving it a new input string to parse. |
| public StringTokenizer | |
| public void | set(final String
this parameter ignored. obj)Implements java. Throws |
| public StringTokenizer | Returns: this instance.the delimiter character to use. delim)Sets the field delimiter character. |
| public StringTokenizer | Returns: this instance.the delimiter matcher to use. delim)Sets the field delimiter matcher. |
| public StringTokenizer | Returns: this instance.the delimiter string to use. delim)Sets the field delimiter string. |
| public StringTokenizer | Returns: this instance.whether empty tokens are returned as null. emptyAsNull)Sets whether the tokenizer should return empty tokens as null. |
| public StringTokenizer | Returns: this instance.the ignored character to use. ignored)Sets the character to ignore. |
| public StringTokenizer | Returns: this instance.the ignored matcher to use, null ignored. ignored)Sets the matcher for characters to ignore. |
| public StringTokenizer | Returns: this instance.whether empty tokens are not returned. ignoreEmptyTokens)Sets whether the tokenizer should ignore and not return empty tokens. |
| public StringTokenizer | Returns: this instance.the quote character to use. quote)Sets the quote character to use. |
| public StringTokenizer | Returns: this instance.the quote matcher to use, null ignored. quote)Sets the quote matcher to use. |
| public StringTokenizer | Returns: this instance.the trimmer matcher to use, null ignored. trimmer)Sets the matcher for characters to trim. |
| public int | |
| protected List | Returns: The modifiable list of String tokens, unmodifiable if null array or zero count.the character array being tokenized, may be null. srcChars, final int the start position within the character array, must be valid. offset, final int the number of characters to tokenize, must be valid. count)Internal method to performs the tokenization. |
| public String | Returns: The string content being parsed.Overrides java. Gets the String content that the tokenizer is parsing. |
| chars | back to summary |
|---|---|
| private char[] chars The text to work on. | |
| CSV_TOKENIZER_PROTOTYPE | back to summary |
|---|---|
| private static final StringTokenizer CSV_TOKENIZER_PROTOTYPE Comma separated values tokenizer internal variable. | |
| delimMatcher | back to summary |
|---|---|
| private StringMatcher delimMatcher The delimiter matcher. | |
| emptyAsNull | back to summary |
|---|---|
| private boolean emptyAsNull Whether to return empty tokens as null. | |
| ignoredMatcher | back to summary |
|---|---|
| private StringMatcher ignoredMatcher The ignored matcher. | |
| ignoreEmptyTokens | back to summary |
|---|---|
| private boolean ignoreEmptyTokens Whether to ignore empty tokens. | |
| quoteMatcher | back to summary |
|---|---|
| private StringMatcher quoteMatcher The quote matcher. | |
| tokenPos | back to summary |
|---|---|
| private int tokenPos The current iteration position. | |
| tokens | back to summary |
|---|---|
| private String[] tokens The parsed tokens. | |
| trimmerMatcher | back to summary |
|---|---|
| private StringMatcher trimmerMatcher The trimmer matcher. | |
| TSV_TOKENIZER_PROTOTYPE | back to summary |
|---|---|
| private static final StringTokenizer TSV_TOKENIZER_PROTOTYPE Tab separated values tokenizer internal variable. | |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer() Constructs a tokenizer splitting on space, tab, newline and form feed as per StringTokenizer, but with no text to tokenize.
This constructor is normally used with | |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final char[] input) Constructs a tokenizer splitting on space, tab, newline and form feed as per StringTokenizer.
| |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final char[] input, final char delim) Constructs a tokenizer splitting on the specified character.
| |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final char[] input, final char delim, final char quote) Constructs a tokenizer splitting on the specified delimiter character and handling quotes using the specified quote character.
| |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final char[] input, final String delim) Constructs a tokenizer splitting on the specified string.
| |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final char[] input, final StringMatcher delim) Constructs a tokenizer splitting using the specified delimiter matcher.
| |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final char[] input, final StringMatcher delim, final StringMatcher quote) Constructs a tokenizer splitting using the specified delimiter matcher and handling quotes using the specified quote matcher.
| |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final String input) Constructs a tokenizer splitting on space, tab, newline and form feed as per StringTokenizer.
| |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final String input, final char delim) Constructs a tokenizer splitting on the specified delimiter character.
| |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final String input, final char delim, final char quote) Constructs a tokenizer splitting on the specified delimiter character and handling quotes using the specified quote character.
| |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final String input, final String delim) Constructs a tokenizer splitting on the specified delimiter string. | |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final String input, final StringMatcher delim) Constructs a tokenizer splitting using the specified delimiter matcher.
| |
| StringTokenizer | back to summary |
|---|---|
| public StringTokenizer(final String input, final StringMatcher delim, final StringMatcher quote) Constructs a tokenizer splitting using the specified delimiter matcher and handling quotes using the specified quote matcher.
| |
| add | back to summary |
|---|---|
| public void add(final String obj) Implements java. Unsupported ListIterator operation.
| |
| addToken | back to summary |
|---|---|
| private void addToken(final List<String> list, String tok) Adds a token to a list, paying attention to the parameters we've set. | |
| checkTokenized | back to summary |
|---|---|
| private void checkTokenized() Checks if tokenization has been done, and if not then do it. | |
| clone | back to summary |
|---|---|
| public Object clone() Overrides java. Creates a new instance of this Tokenizer. The new instance is reset so that it will be at the start of the token list. If a
| |
| cloneReset | back to summary |
|---|---|
| pack-priv Object cloneReset() throws CloneNotSupportedException Creates a new instance of this Tokenizer. The new instance is reset so that it will be at the start of the token list.
| |
| getContent | back to summary |
|---|---|
| public String getContent() Gets the String content that the tokenizer is parsing.
| |
| getCSVClone | back to summary |
|---|---|
| private static StringTokenizer getCSVClone() Returns a clone of
| |
| getCSVInstance | back to summary |
|---|---|
| public static StringTokenizer getCSVInstance() Gets a new tokenizer instance which parses Comma Separated Value strings initializing it with the given input. The default for CSV processing will be trim whitespace from both ends (which can be overridden with the setTrimmer method). You must call a "reset" method to set the string which you want to parse.
| |
| getCSVInstance | back to summary |
|---|---|
| public static StringTokenizer getCSVInstance(final char[] input) Gets a new tokenizer instance which parses Comma Separated Value strings initializing it with the given input. The default for CSV processing will be trim whitespace from both ends (which can be overridden with the setTrimmer method).
| |
| getCSVInstance | back to summary |
|---|---|
| public static StringTokenizer getCSVInstance(final String input) Gets a new tokenizer instance which parses Comma Separated Value strings initializing it with the given input. The default for CSV processing will be trim whitespace from both ends (which can be overridden with the setTrimmer method).
| |
| getDelimiterMatcher | back to summary |
|---|---|
| public StringMatcher getDelimiterMatcher() Gets the field delimiter matcher.
| |
| getIgnoredMatcher | back to summary |
|---|---|
| public StringMatcher getIgnoredMatcher() Gets the ignored character matcher. These characters are ignored when parsing the String, unless they are within a quoted region. The default value is not to ignore anything.
| |
| getQuoteMatcher | back to summary |
|---|---|
| public StringMatcher getQuoteMatcher() Gets the quote matcher currently in use. The quote character is used to wrap data between the tokens. This enables delimiters to be entered as data. The default value is '"' (double quote).
| |
| getTokenArray | back to summary |
|---|---|
| public String[] getTokenArray() Gets a copy of the full token list as an independent modifiable array.
| |
| getTokenList | back to summary |
|---|---|
| public List Gets a copy of the full token list as an independent modifiable list. | |
| getTrimmerMatcher | back to summary |
|---|---|
| public StringMatcher getTrimmerMatcher() Gets the trimmer character matcher. These characters are trimmed off on each side of the delimiter until the token or quote is found. The default value is not to trim anything.
| |
| getTSVClone | back to summary |
|---|---|
| private static StringTokenizer getTSVClone() Returns a clone of
| |
| getTSVInstance | back to summary |
|---|---|
| public static StringTokenizer getTSVInstance() Gets a new tokenizer instance which parses Tab Separated Value strings. The default for CSV processing will be trim whitespace from both ends (which can be overridden with the setTrimmer method). You must call a "reset" method to set the string which you want to parse.
| |
| getTSVInstance | back to summary |
|---|---|
| public static StringTokenizer getTSVInstance(final char[] input) Gets a new tokenizer instance which parses Tab Separated Value strings. The default for CSV processing will be trim whitespace from both ends (which can be overridden with the setTrimmer method).
| |
| getTSVInstance | back to summary |
|---|---|
| public static StringTokenizer getTSVInstance(final String input) Gets a new tokenizer instance which parses Tab Separated Value strings. The default for CSV processing will be trim whitespace from both ends (which can be overridden with the setTrimmer method).
| |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Tests whether there are any more tokens.
| |
| hasPrevious | back to summary |
|---|---|
| public boolean hasPrevious() Implements java. Tests whether there are any previous tokens that can be iterated to.
| |
| isEmptyTokenAsNull | back to summary |
|---|---|
| public boolean isEmptyTokenAsNull() Tests whether the tokenizer currently returns empty tokens as null. The default for this property is false.
| |
| isIgnoreEmptyTokens | back to summary |
|---|---|
| public boolean isIgnoreEmptyTokens() Tests whether the tokenizer currently ignores empty tokens. The default for this property is true.
| |
| isQuote | back to summary |
|---|---|
| private boolean isQuote(final char[] srcChars, final int pos, final int len, final int quoteStart, final int quoteLen) Tests if the characters at the index specified match the quote already matched in readNextToken().
| |
| next | back to summary |
|---|---|
| public String next() Implements java. Gets the next token.
| |
| nextIndex | back to summary |
|---|---|
| public int nextIndex() Implements java. Gets the index of the next token to return.
| |
| nextToken | back to summary |
|---|---|
| public String nextToken() Gets the next token from the String. Equivalent to
| |
| previous | back to summary |
|---|---|
| public String previous() Implements java. Gets the token previous to the last returned token. | |
| previousIndex | back to summary |
|---|---|
| public int previousIndex() Implements java. Gets the index of the previous token.
| |
| previousToken | back to summary |
|---|---|
| public String previousToken() Gets the previous token from the String.
| |
| readNextToken | back to summary |
|---|---|
| private int readNextToken(final char[] srcChars, int start, final int len, final TextStringBuilder workArea, final List<String> tokenList)
References Deprecated
See corresponding docs for further information. Reads character by character through the String to get the next token.
| |
| readWithQuotes | back to summary |
|---|---|
| private int readWithQuotes(final char[] srcChars, final int start, final int len, final TextStringBuilder workArea, final List<String> tokenList, final int quoteStart, final int quoteLen)
References Deprecated
See corresponding docs for further information. Reads a possibly quoted string token.
| |
| remove | back to summary |
|---|---|
| public void remove() Implements java. Throws
| |
| reset | back to summary |
|---|---|
| public StringTokenizer reset() Resets this tokenizer, forgetting all parsing and iteration already completed. This method allows the same tokenizer to be reused for the same String.
| |
| reset | back to summary |
|---|---|
| public StringTokenizer reset(final char[] input) Resets this tokenizer, giving it a new input string to parse. In this manner you can re-use a tokenizer with the same settings on multiple input lines.
| |
| reset | back to summary |
|---|---|
| public StringTokenizer reset(final String input) Resets this tokenizer, giving it a new input string to parse. In this manner you can re-use a tokenizer with the same settings on multiple input lines.
| |
| set | back to summary |
|---|---|
| public void set(final String obj) Implements java. Throws
| |
| setDelimiterChar | back to summary |
|---|---|
| public StringTokenizer setDelimiterChar(final char delim) Sets the field delimiter character.
| |
| setDelimiterMatcher | back to summary |
|---|---|
| public StringTokenizer setDelimiterMatcher(final StringMatcher delim) Sets the field delimiter matcher. The delimiter is used to separate one token from another.
| |
| setDelimiterString | back to summary |
|---|---|
| public StringTokenizer setDelimiterString(final String delim) Sets the field delimiter string.
| |
| setEmptyTokenAsNull | back to summary |
|---|---|
| public StringTokenizer setEmptyTokenAsNull(final boolean emptyAsNull) Sets whether the tokenizer should return empty tokens as null. The default for this property is false.
| |
| setIgnoredChar | back to summary |
|---|---|
| public StringTokenizer setIgnoredChar(final char ignored) Sets the character to ignore. This character is ignored when parsing the String, unless it is within a quoted region.
| |
| setIgnoredMatcher | back to summary |
|---|---|
| public StringTokenizer setIgnoredMatcher(final StringMatcher ignored) Sets the matcher for characters to ignore. These characters are ignored when parsing the String, unless they are within a quoted region.
| |
| setIgnoreEmptyTokens | back to summary |
|---|---|
| public StringTokenizer setIgnoreEmptyTokens(final boolean ignoreEmptyTokens) Sets whether the tokenizer should ignore and not return empty tokens. The default for this property is true.
| |
| setQuoteChar | back to summary |
|---|---|
| public StringTokenizer setQuoteChar(final char quote) Sets the quote character to use. The quote character is used to wrap data between the tokens. This enables delimiters to be entered as data.
| |
| setQuoteMatcher | back to summary |
|---|---|
| public StringTokenizer setQuoteMatcher(final StringMatcher quote) Sets the quote matcher to use. The quote character is used to wrap data between the tokens. This enables delimiters to be entered as data.
| |
| setTrimmerMatcher | back to summary |
|---|---|
| public StringTokenizer setTrimmerMatcher(final StringMatcher trimmer) Sets the matcher for characters to trim. These characters are trimmed off on each side of the delimiter until the token or quote is found.
| |
| size | back to summary |
|---|---|
| public int size() Gets the number of tokens found in the String.
| |
| tokenize | back to summary |
|---|---|
| protected List Internal method to performs the tokenization. Most users of this class do not need to call this method. This method will be called automatically by other (public) methods when required. This method exists to allow subclasses to add code before or after the tokenization. For example, a subclass could alter the character array, offset or count to be parsed, or call the tokenizer multiple times on multiple strings. It is also be possible to filter the results.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Gets the String content that the tokenizer is parsing. | |