| java.lang.Object org.apache.commons.lang.text.StrTokenizer
StrTokenizer | public class StrTokenizer implements ListIterator,Cloneable(Code) | | Tokenizes a string based based on delimiters (separators)
and supporting quoting and ignored character concepts.
This class can split a String into many smaller strings. It aims
to do a similar job to
java.util.StringTokenizer StringTokenizer ,
however it offers much more control and flexibility including implementing
the ListIterator interface. By default, it is setup
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)
This tokenizer has the following properties and options:
Property | Type | Default |
delim | CharSetMatcher | { \t\n\r\f} |
quote | NoneMatcher | {} |
ignore | NoneMatcher | {} |
emptyTokenAsNull | boolean | false |
ignoreEmptyTokens | boolean | true |
author: Matthew Inger author: Stephen Colebourne author: Gary D. Gregory since: 2.2 version: $Id: StrTokenizer.java 491653 2007-01-01 22:03:58Z ggregory $ |
Constructor Summary | |
public | StrTokenizer() Constructs a tokenizer splitting on space, tab, newline and formfeed
as per StringTokenizer, but with no text to tokenize. | public | StrTokenizer(String input) Constructs a tokenizer splitting on space, tab, newline and formfeed
as per StringTokenizer. | public | StrTokenizer(String input, char delim) Constructs a tokenizer splitting on the specified delimiter character. | public | StrTokenizer(String input, String delim) Constructs a tokenizer splitting on the specified delimiter string. | public | StrTokenizer(String input, StrMatcher delim) Constructs a tokenizer splitting using the specified delimiter matcher. | public | StrTokenizer(String input, char delim, char quote) Constructs a tokenizer splitting on the specified delimiter character
and handling quotes using the specified quote character. | public | StrTokenizer(String input, StrMatcher delim, StrMatcher quote) Constructs a tokenizer splitting using the specified delimiter matcher
and handling quotes using the specified quote matcher. | public | StrTokenizer(char[] input) Constructs a tokenizer splitting on space, tab, newline and formfeed
as per StringTokenizer. | public | StrTokenizer(char[] input, char delim) Constructs a tokenizer splitting on the specified character. | public | StrTokenizer(char[] input, String delim) Constructs a tokenizer splitting on the specified string. | public | StrTokenizer(char[] input, StrMatcher delim) Constructs a tokenizer splitting using the specified delimiter matcher. | public | StrTokenizer(char[] input, char delim, char quote) Constructs a tokenizer splitting on the specified delimiter character
and handling quotes using the specified quote character. | public | StrTokenizer(char[] input, StrMatcher delim, StrMatcher quote) Constructs a tokenizer splitting using the specified delimiter matcher
and handling quotes using the specified quote matcher. |
StrTokenizer | public StrTokenizer()(Code) | | Constructs a tokenizer splitting on space, tab, newline and formfeed
as per StringTokenizer, but with no text to tokenize.
This constructor is normally used with
StrTokenizer.reset(String) .
|
StrTokenizer | public StrTokenizer(String input)(Code) | | Constructs a tokenizer splitting on space, tab, newline and formfeed
as per StringTokenizer.
Parameters: input - the string which is to be parsed |
StrTokenizer | public StrTokenizer(String input, char delim)(Code) | | Constructs a tokenizer splitting on the specified delimiter character.
Parameters: input - the string which is to be parsed Parameters: delim - the field delimiter character |
StrTokenizer | public StrTokenizer(String input, String delim)(Code) | | Constructs a tokenizer splitting on the specified delimiter string.
Parameters: input - the string which is to be parsed Parameters: delim - the field delimiter string |
StrTokenizer | public StrTokenizer(String input, StrMatcher delim)(Code) | | Constructs a tokenizer splitting using the specified delimiter matcher.
Parameters: input - the string which is to be parsed Parameters: delim - the field delimiter matcher |
StrTokenizer | public StrTokenizer(String input, char delim, char quote)(Code) | | Constructs a tokenizer splitting on the specified delimiter character
and handling quotes using the specified quote character.
Parameters: input - the string which is to be parsed Parameters: delim - the field delimiter character Parameters: quote - the field quoted string character |
StrTokenizer | public StrTokenizer(String input, StrMatcher delim, StrMatcher quote)(Code) | | Constructs a tokenizer splitting using the specified delimiter matcher
and handling quotes using the specified quote matcher.
Parameters: input - the string which is to be parsed Parameters: delim - the field delimiter matcher Parameters: quote - the field quoted string matcher |
StrTokenizer | public StrTokenizer(char[] input)(Code) | | Constructs a tokenizer splitting on space, tab, newline and formfeed
as per StringTokenizer.
The input character array is not cloned, and must not be altered after
passing in to this method.
Parameters: input - the string which is to be parsed, not cloned |
StrTokenizer | public StrTokenizer(char[] input, char delim)(Code) | | Constructs a tokenizer splitting on the specified character.
The input character array is not cloned, and must not be altered after
passing in to this method.
Parameters: input - the string which is to be parsed, not cloned Parameters: delim - the field delimiter character |
StrTokenizer | public StrTokenizer(char[] input, String delim)(Code) | | Constructs a tokenizer splitting on the specified string.
The input character array is not cloned, and must not be altered after
passing in to this method.
Parameters: input - the string which is to be parsed, not cloned Parameters: delim - the field delimiter string |
StrTokenizer | public StrTokenizer(char[] input, StrMatcher delim)(Code) | | Constructs a tokenizer splitting using the specified delimiter matcher.
The input character array is not cloned, and must not be altered after
passing in to this method.
Parameters: input - the string which is to be parsed, not cloned Parameters: delim - the field delimiter matcher |
StrTokenizer | public StrTokenizer(char[] input, char delim, char quote)(Code) | | Constructs a tokenizer splitting on the specified delimiter character
and handling quotes using the specified quote character.
The input character array is not cloned, and must not be altered after
passing in to this method.
Parameters: input - the string which is to be parsed, not cloned Parameters: delim - the field delimiter character Parameters: quote - the field quoted string character |
StrTokenizer | public StrTokenizer(char[] input, StrMatcher delim, StrMatcher quote)(Code) | | Constructs a tokenizer splitting using the specified delimiter matcher
and handling quotes using the specified quote matcher.
The input character array is not cloned, and must not be altered after
passing in to this method.
Parameters: input - the string which is to be parsed, not cloned Parameters: delim - the field delimiter character Parameters: quote - the field quoted string character |
clone | public Object clone()(Code) | | 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
CloneNotSupportedException is caught, return null .
a new instance of this Tokenizer which has been reset. |
getCSVInstance | public static StrTokenizer getCSVInstance()(Code) | | Gets a new tokenizer instance which parses Comma Seperated Value strings
initializing it with the given input. The default for CSV processing
will be trim whitespace from both ends (which can be overriden with
the setTrimmer method).
You must call a "reset" method to set the string which you want to parse.
a new tokenizer instance which parses Comma Seperated Value strings |
getCSVInstance | public static StrTokenizer getCSVInstance(String input)(Code) | | Gets a new tokenizer instance which parses Comma Seperated Value strings
initializing it with the given input. The default for CSV processing
will be trim whitespace from both ends (which can be overriden with
the setTrimmer method).
Parameters: input - the text to parse a new tokenizer instance which parses Comma Seperated Value strings |
getCSVInstance | public static StrTokenizer getCSVInstance(char[] input)(Code) | | Gets a new tokenizer instance which parses Comma Seperated Value strings
initializing it with the given input. The default for CSV processing
will be trim whitespace from both ends (which can be overriden with
the setTrimmer method).
Parameters: input - the text to parse a new tokenizer instance which parses Comma Seperated Value strings |
getContent | public String getContent()(Code) | | Gets the String content that the tokenizer is parsing.
the string content being parsed |
getDelimiterMatcher | public StrMatcher getDelimiterMatcher()(Code) | | Gets the field delimiter matcher.
the delimiter matcher in use |
getIgnoredMatcher | public StrMatcher getIgnoredMatcher()(Code) | | 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.
the ignored matcher in use |
getQuoteMatcher | public StrMatcher getQuoteMatcher()(Code) | | 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).
the quote matcher in use |
getTSVInstance | public static StrTokenizer getTSVInstance()(Code) | | Gets a new tokenizer instance which parses Tab Seperated Value strings.
The default for CSV processing will be trim whitespace from both ends
(which can be overriden with the setTrimmer method).
You must call a "reset" method to set the string which you want to parse.
a new tokenizer instance which parses Tab Seperated Value strings. |
getTSVInstance | public static StrTokenizer getTSVInstance(String input)(Code) | | Gets a new tokenizer instance which parses Tab Seperated Value strings.
The default for CSV processing will be trim whitespace from both ends
(which can be overriden with the setTrimmer method).
Parameters: input - the string to parse a new tokenizer instance which parses Tab Seperated Value strings. |
getTSVInstance | public static StrTokenizer getTSVInstance(char[] input)(Code) | | Gets a new tokenizer instance which parses Tab Seperated Value strings.
The default for CSV processing will be trim whitespace from both ends
(which can be overriden with the setTrimmer method).
Parameters: input - the string to parse a new tokenizer instance which parses Tab Seperated Value strings. |
getTokenArray | public String[] getTokenArray()(Code) | | Gets a copy of the full token list as an independent modifiable array.
the tokens as a String array |
getTokenList | public List getTokenList()(Code) | | Gets a copy of the full token list as an independent modifiable list.
the tokens as a String array |
getTrimmerMatcher | public StrMatcher getTrimmerMatcher()(Code) | | 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.
the trimmer matcher in use |
hasNext | public boolean hasNext()(Code) | | Checks whether there are any more tokens.
true if there are more tokens |
hasPrevious | public boolean hasPrevious()(Code) | | Checks whether there are any previous tokens that can be iterated to.
true if there are previous tokens |
isEmptyTokenAsNull | public boolean isEmptyTokenAsNull()(Code) | | Gets whether the tokenizer currently returns empty tokens as null.
The default for this property is false.
true if empty tokens are returned as null |
isIgnoreEmptyTokens | public boolean isIgnoreEmptyTokens()(Code) | | Gets whether the tokenizer currently ignores empty tokens.
The default for this property is false.
true if empty tokens are not returned |
nextIndex | public int nextIndex()(Code) | | Gets the index of the next token to return.
the next token index |
nextToken | public String nextToken()(Code) | | Gets the next token from the String.
the next sequential token, or null when no more tokens are found |
previous | public Object previous()(Code) | | Gets the token previous to the last returned token.
the previous token |
previousIndex | public int previousIndex()(Code) | | Gets the index of the previous token.
the previous token index |
previousToken | public String previousToken()(Code) | | Gets the previous token from the String.
the previous sequential token, or null when no more tokens are found |
reset | public StrTokenizer reset()(Code) | | Resets this tokenizer, forgetting all parsing and iteration already completed.
This method allows the same tokenizer to be reused for the same String.
this, to enable chaining |
reset | public StrTokenizer reset(String input)(Code) | | Reset 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.
Parameters: input - the new string to tokenize, null sets no text to parse this, to enable chaining |
reset | public StrTokenizer reset(char[] input)(Code) | | Reset 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.
The input character array is not cloned, and must not be altered after
passing in to this method.
Parameters: input - the new character array to tokenize, not cloned, null sets no text to parse this, to enable chaining |
setDelimiterChar | public StrTokenizer setDelimiterChar(char delim)(Code) | | Sets the field delimiter character.
Parameters: delim - the delimiter character to use this, to enable chaining |
setDelimiterMatcher | public StrTokenizer setDelimiterMatcher(StrMatcher delim)(Code) | | Sets the field delimiter matcher.
The delimitier is used to separate one token from another.
Parameters: delim - the delimiter matcher to use this, to enable chaining |
setDelimiterString | public StrTokenizer setDelimiterString(String delim)(Code) | | Sets the field delimiter string.
Parameters: delim - the delimiter string to use this, to enable chaining |
setEmptyTokenAsNull | public StrTokenizer setEmptyTokenAsNull(boolean emptyAsNull)(Code) | | Sets whether the tokenizer should return empty tokens as null.
The default for this property is false.
Parameters: emptyAsNull - whether empty tokens are returned as null this, to enable chaining |
setIgnoreEmptyTokens | public StrTokenizer setIgnoreEmptyTokens(boolean ignoreEmptyTokens)(Code) | | Sets whether the tokenizer should ignore and not return empty tokens.
The default for this property is false.
Parameters: ignoreEmptyTokens - whether empty tokens are not returned this, to enable chaining |
setIgnoredChar | public StrTokenizer setIgnoredChar(char ignored)(Code) | | Set the character to ignore.
This character is ignored when parsing the String, unless it is
within a quoted region.
Parameters: ignored - the ignored character to use this, to enable chaining |
setIgnoredMatcher | public StrTokenizer setIgnoredMatcher(StrMatcher ignored)(Code) | | Set the matcher for characters to ignore.
These characters are ignored when parsing the String, unless they are
within a quoted region.
Parameters: ignored - the ignored matcher to use, null ignored this, to enable chaining |
setQuoteChar | public StrTokenizer setQuoteChar(char quote)(Code) | | 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.
Parameters: quote - the quote character to use this, to enable chaining |
setQuoteMatcher | public StrTokenizer setQuoteMatcher(StrMatcher quote)(Code) | | Set the quote matcher to use.
The quote character is used to wrap data between the tokens.
This enables delimiters to be entered as data.
Parameters: quote - the quote matcher to use, null ignored this, to enable chaining |
setTrimmerMatcher | public StrTokenizer setTrimmerMatcher(StrMatcher trimmer)(Code) | | 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.
Parameters: trimmer - the trimmer matcher to use, null ignored this, to enable chaining |
size | public int size()(Code) | | Gets the number of tokens found in the String.
the number of matched tokens |
toString | public String toString()(Code) | | Gets the String content that the tokenizer is parsing.
the string content being parsed |
tokenize | protected List tokenize(char[] chars, int offset, int count)(Code) | | 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.
StrTokenizer will always pass a zero offset and a count
equal to the length of the array to this method, however a subclass
may pass other values, or even an entirely different array.
Parameters: chars - the character array being tokenized, may be null Parameters: offset - the start position within the character array, must be valid Parameters: count - the number of characters to tokenize, must be valid the modifiable list of String tokens, unmodifiable if null array or zero count |
|
|