StringTokenizer in java

StringTokenizer class is used to break a string into tokens using specified delimiter.
Note: space is the default delimiter.

Constructors of StringTokenizer class:

1. public StringTokenizer(String str): creates a string tokenizer for the specified string. By default space is taken as delimiter here.

2. public StringTokenizer(String str, String delimiter): creates a string tokenizer for the specified string by using specified delimiter. Delimiter character will be treated as separator only and not be treated as tokens.

3. public StringTokenizer(String str, String delimiter, boolean returnDelimiters): creates a string tokenizer for the specified string by using specified delimiter. Delimiter character can be treated as separator returnDelimiters is true.

Commonly used methods of StringTokenizer:

1. hasMoreTokens(): checks whether more tokens are available or not. It returns true if more takens are available else return false.

Syntax:

public boolean hasMoreTokens()

2. hasMoreElements():  same as hasMoreTokens() , mainly used in case of enumeration.

Syntax:

public boolean hasMoreElements()

3. nextToken(): returns the next token from this string tokenizer.
Note: It will throw NoSuchElementException if no more token is available.
Syntax:

public String nextToken()

4. nextToken(String delimiter): returns the next token from this string tokenizer using specified delimeter. It is mainly used when we want to separate tokens using differen delimiters in single string tokenizer.

Syntax:

public String nextToken(String delim)

Note: It will throw NoSuchElementException if no more token is available.

5. nextElement(): same as nextToken() except that it returns an object rather than string. It is mainly used in case of enumeration.

Syntax:

public Object nextElement()

Note: It will throw NoSuchElementException if no more token is available.

6. countTokens(): returns the number of times nextToken method can be called on this string tokenizer before an exception occur.

Syntax:

public int countTokens()

Example 1:

StringTokenizerExample.java

import java.util.StringTokenizer;
 
/**
 * This program is used to print all tokens of a string.
 * @author w3spoint
 */
class TestStringTokenizer{
	//By default whitespace will act as separator.
	StringTokenizer str = new StringTokenizer("Hello i am here");
 
	/**
	 * This method is used to print all tokens of a string.
	 * @author w3spoint
	 */
	public void displayTokens(){
		while(str.hasMoreTokens()){
			System.out.println(str.nextToken());
		}
	}
}
 
public class StringTokenizerExample {
	public static void main(String args[]){
		//creating TestStringTokenizer object.
		TestStringTokenizer obj = new TestStringTokenizer();
 
		//method call
		obj.displayTokens();		
	}
}

Output:

Hello
i
am
here

Download this example.

Example 2:

StringTokenizerExample.java

import java.util.StringTokenizer;
 
/**
 * This program is used to print all tokens 
 * of a string on the bases of comma.
 * @author w3spoint
 */
class TestStringTokenizer{
	//Using comma as separator.
	StringTokenizer str = 
               new StringTokenizer("Hello ,i ,am ,here", ",");
 
	/**
	 * This method is used to print all tokens 
         * of a string on the bases of comma.
	 * @author w3spoint
	 */
	public void displayTokens(){
		while(str.hasMoreTokens()){
			System.out.println(str.nextToken());
		}
	}
}
 
public class StringTokenizerExample {
	public static void main(String args[]){
		//creating TestStringTokenizer object.
		TestStringTokenizer obj = new TestStringTokenizer();
 
		//method call
		obj.displayTokens();		
	}
}

Output:

Hello
i
am
here

Download this example.

Example 3:

StringTokenizerExample.java

import java.util.StringTokenizer;
 
/**
 * This program is used to print all tokens of 
 * a string on the bases of multiple separators.
 * @author w3spoint
 */
class TestStringTokenizer{
	//Using multiple separators.
	StringTokenizer str = 
               new StringTokenizer("Hello ,i ;am :here", ", ; :");
 
	/**
	 * This method is used to print all tokens of 
         * a string on the bases of multiple separators.
	 * @author w3spoint
	 */
	public void displayTokens(){
		while(str.hasMoreTokens()){
			System.out.println(str.nextToken());
		}
	}
}
 
public class StringTokenizerExample {
	public static void main(String args[]){
		//creating TestStringTokenizer object.
		TestStringTokenizer obj = new TestStringTokenizer();
 
		//method call
		obj.displayTokens();		
	}
}

Output:

Hello
i
am
here

Download this example.
 
Next Topic: StringBuilder in java.
Previous Topic: ensureCapacity(int minCapacity) StringBuffer method in java.

 

Content Protection by DMCA.com
Please Share