|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectgov.nih.nlm.util.FieldedStringTokenizer
Breaks character deliminted strings into constituent fields.
Unlike the StringTokenizer
it recognizes empty tokens.
For example, using a pipe character '|' as a delimiter, it would
tokenize the string "a||b" into three tokens "a", "", "b". Following
is an example of how to use this class.
FieldedStringTokenizer tokenizer = new FieldedStringTokenizer("a|b||c","|"); int i = 1; while (tokenizer.hasMoreTokens()) { System.out.println("Token " + i + " is: " + tokenizer.nextToken()); }This class can also be used in a static way to split a line on a particular character delmiter and have a
String[]
passed back. For example,
String[] tokens = FieldedStringTokenizer.split("a|b|c|d","|");This code will produce the string array
{"a","b","c","d"}
.
Constructor Summary | |
FieldedStringTokenizer(String str,
String delim)
Instantiates a FieldedStringTokenizer for the specified string. |
Method Summary | |
int |
countTokens()
Calculates the number of times that this tokenizer's nextToken()
method can be called before it generates an exception. |
boolean |
hasMoreElements()
Returns the same value as the hasMoreTokens() method. |
boolean |
hasMoreTokens()
Tests if there are more tokens available from this tokenizer's string. |
static void |
main(String[] args)
Self-qa test. |
Object |
nextElement()
Returns the same value as the nextToken() method,
except that its declared return value is Object
rather than String . |
String |
nextToken()
Returns the next token in this tokenizer's string. |
static String[] |
split(String line,
String delim)
Splits a line on delimiter characters and returns a String [] of the tokens. |
static String[] |
split(String line,
String delim,
int field_ct)
Splits a line on delimiter characters when the number of fields is known in advance and returns a string array of the tokens. |
static void |
split(String line,
String delim,
int field_ct,
String[] tokens)
Splits a line on delimiter characters when the number of fields is known in advance and populates the specified String[]. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public FieldedStringTokenizer(String str, String delim)
FieldedStringTokenizer
for the specified string.
The characters in the delim
argument are the delimiters for separating tokens. Delimiter characters
themselves will not be treated as tokens. This tokenizer will return
null tokens for adjacent delimiters.
str
- a String
to be tokenizeddelim
- a String
containing delimiter charactersMethod Detail |
public static String[] split(String line, String delim)
String
[]
of the tokens.
line
- a String
to be splitdelim
- a String
containing delimiter characters
String []
containing tokens from the stringpublic static String[] split(String line, String delim, int field_ct)
line
- a String
to be splitdelim
- a String
containing delimiter charactersfield_ct
- the number of fields in the line to be split.
String []
containing tokens from the stringpublic static void split(String line, String delim, int field_ct, String[] tokens)
line
- a String
to be splitdelim
- a String
containing delimiter charactersfield_ct
- int
indicates the max number of fields to splittokens
- a String
[] of the right number of tokens.public int countTokens()
nextToken()
method can be called before it generates an exception. The
current position is not advanced.
int
count of the number of tokenspublic boolean hasMoreElements()
hasMoreTokens()
method.
It exists so that this class can implement the
Enumeration
interface.
hasMoreElements
in interface Enumeration
true
if there are more tokens;
false
otherwisepublic boolean hasMoreTokens()
nextToken()
will
successfully return a token.
true
if there are more tokens;
false
otherwisepublic Object nextElement()
nextToken()
method,
except that its declared return value is Object
rather than String
. It exists so that this class
can implement the Enumeration
interface.
nextElement
in interface Enumeration
String
representation of the next tokenpublic String nextToken()
public static void main(String[] args)
args
- a String
[]
containing
a string to split and a delimiter set
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |