gov.nih.nlm.util
Class FieldedStringTokenizer

java.lang.Object
  extended bygov.nih.nlm.util.FieldedStringTokenizer
All Implemented Interfaces:
Enumeration

public class FieldedStringTokenizer
extends Object
implements Enumeration

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"}.

Author:
MEME Group

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

FieldedStringTokenizer

public FieldedStringTokenizer(String str,
                              String delim)
Instantiates a 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.

Parameters:
str - a String to be tokenized
delim - a String containing delimiter characters
Method Detail

split

public static String[] split(String line,
                             String delim)
Splits a line on delimiter characters and returns a String[] of the tokens.

Parameters:
line - a String to be split
delim - a String containing delimiter characters
Returns:
a String [] containing tokens from the string

split

public 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.

Parameters:
line - a String to be split
delim - a String containing delimiter characters
field_ct - the number of fields in the line to be split.
Returns:
a String [] containing tokens from the string

split

public 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[].

Parameters:
line - a String to be split
delim - a String containing delimiter characters
field_ct - int indicates the max number of fields to split
tokens - a String[] of the right number of tokens.

countTokens

public int countTokens()
Calculates the number of times that this tokenizer's nextToken() method can be called before it generates an exception. The current position is not advanced.

Returns:
an int count of the number of tokens

hasMoreElements

public boolean hasMoreElements()
Returns the same value as the hasMoreTokens() method. It exists so that this class can implement the Enumeration interface.

Specified by:
hasMoreElements in interface Enumeration
Returns:
true if there are more tokens; false otherwise

hasMoreTokens

public boolean hasMoreTokens()
Tests if there are more tokens available from this tokenizer's string. If this method returns true, then a subsequent call to nextToken() will successfully return a token.

Returns:
true if there are more tokens; false otherwise

nextElement

public Object nextElement()
Returns the same value as the nextToken() method, except that its declared return value is Object rather than String. It exists so that this class can implement the Enumeration interface.

Specified by:
nextElement in interface Enumeration
Returns:
An object String representation of the next token

nextToken

public String nextToken()
Returns the next token in this tokenizer's string.

Returns:
the next token in this tokeinzer's string

main

public static void main(String[] args)
Self-qa test.

Parameters:
args - a String[] containing a string to split and a delimiter set


Copyright ©2005