Java: StringTokenizer class with example

In this article, we will discuss StringTokenizer class in detail and how it helps in splitting String based on various delimiter

 

StringToknizer class:

  • This class allows to divide complete String into number of tokens
  • There are 3 variants for constructing StringTokenizer object viz.;
  • 1st variant takes 1-argument i.e.; String which needs to be parsed into number of tokens
  • Whereas 2nd variant takes 2-arguments i.e.; String to be parsed into tokens and delimiter
  • Note: if no delimiter is specified, then by default space as assumed as delimiter, as in the case of 1st variant
  • 3rd variant is rarely used where first two arguments are very same as that of 2nd variant and third argument is a boolean flag describing whether to return delimiter or not

 

StringTokenizer Constructor:

Constructor

Description

public StringTokenizer(String str); Constructs new StringTokenizer object for the specified string value

Note: default delimiter is space for parsing and breaking into number of tokens

public StringTokenizer(String str, String delim); Constructs new StringTokenizer object for the specified string value and,

It is parsed & divided into number of tokens based on the specified delimiter

public StringTokenizer(String str, String delim, boolean returnDelims); Constructs new StringTokenizer object for the specified string value and,

It is parsed & divided into number of tokens based on the specified delimiter

Note: specified Boolean flag tells whether to return delimiter or not

 

StringTokenizer Methods:

Methods

Description

public int countTokens(); Returns number of tokens remaining in the string using the current delimiter set
public boolean hasMoreTokens(); Returns true if and only if there is at least one token in the string after the current position;

Otherwise, returns false

public boolean hasMoreElements(); Very similar to hasMoreTokens() method

Returns true, if there are more tokens;

Otherwise returns false

public String nextToken(); Returns next token from StringTokenizer object
public Object nextElement(); Very similar to nextToken() method

Returns next token from StringTokenizer object

public String nextToken(String delim); This is very typical, as it returns next token based on the specified delimiter

 

StringTokenizer example based on various delimiter:

  1. Using space (by default)
  2. Using comma (,)
  3. Using DoT or period (.)
  4. Using pipe character (|)
  5. Using colon (:)

Note:

 

Let us move forward and discuss all examples with above mentioned delimiter

1. StringTokenizer example using space as delimiter

  • By default, String is split on the basis of space, if no delimiter is specified when StringToknizer class is used

Syntax:

StringTokenizer st1 = new StringTokenizer(str);

SplitStringUsingSpaceAsDelimiter.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.StringTokenizer;

public class SplitStringUsingSpaceAsDelimiter {

	public static void main(String[] args) {

		// sample string
		String str = "World consists of"
				+ " both Good and Bad elements";

		System.out.println("Splitting String"
				+ " using space as delimiter :\n");

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(str);

		// iterate through StringTokenizer tokens
		while(st.hasMoreTokens()) {
			System.out.println(st.nextToken());
		}
	}
}

Output

Splitting String using space as delimiter :

World
consists
of
both
Good
and
Bad
elements

 

2. StringTokenizer example using comma as delimiter

  • In this case, we are explicitly specifying comma (,) as delimiter, in the 2nd argument

Syntax:

StringTokenizer st2 = new StringTokenizer(str, “,”);

SplitStringUsingCommaAsDelimiter.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.StringTokenizer;

public class SplitStringUsingCommaAsDelimiter {

	public static void main(String[] args) {

		// sample string
		String str = "Apple,Orange,Banana,Dates,Juice";

		System.out.println("Splitting String"
				+ " using comma as delimiter :\n");

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(str, ",");

		// iterate through StringTokenizer tokens
		while(st.hasMoreTokens()) {
			System.out.println(st.nextToken());
		}
	}
}

Output

Splitting String using comma as delimiter :

Apple
Orange
Banana
Dates
Juice

 

3. StringTokenizer example using DoT or period as delimiter

  • In this case, we are explicitly specifying dot (.) as delimiter, in the 2nd argument

Syntax:

StringTokenizer st3 = new StringTokenizer(str, “.”);

SplitStringUsingDotAsDelimiter.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.StringTokenizer;

public class SplitStringUsingDotAsDelimiter {

	public static void main(String[] args) {

		// sample string
		String str = "in.bench.resources"
				+ ".java.stringtokenizer.example";

		System.out.println("Splitting String"
				+ " using dot/period as delimiter :\n");

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(str, ".");

		// iterate through StringTokenizer tokens
		while(st.hasMoreTokens()) {
			System.out.println(st.nextToken());
		}
	}
}

Output

Splitting String using dot/period as delimiter :

in
bench
resources
java
stringtokenizer
example

 

4. StringTokenizer example using pipe as delimiter

  • In this case, we are explicitly specifying pipe (|) as delimiter, in the 2nd argument

Syntax:

StringTokenizer st4 = new StringTokenizer(str, “|”);

SplitStringUsingPipeAsDelimiter.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.StringTokenizer;

public class SplitStringUsingPipeAsDelimiter {

	public static void main(String[] args) {

		// sample string
		String str = "productId|productName"
				+ "|productDesc|productPrice|productDiscount";

		System.out.println("Splitting String"
				+ " using pipe as delimiter :\n");

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(str, "|");

		// iterate through StringTokenizer tokens
		while(st.hasMoreTokens()) {
			System.out.println(st.nextToken());
		}
	}
}

Output

Splitting String using pipe as delimiter :

productId
productName
productDesc
productPrice
productDiscount

 

5. StringTokenizer example using colon as delimiter

  • In this case, we are explicitly specifying colon (:) as delimiter, in the 2nd argument

Syntax:

StringTokenizer st5 = new StringTokenizer(str, “:”);

SplitStringUsingColonAsDelimiter.java

package in.bench.resources.java.stringtokenizer.example;

import java.util.StringTokenizer;

public class SplitStringUsingColonAsDelimiter {

	public static void main(String[] args) {

		// sample string
		String str = "100123:MotoG:4G-mobile:14520:10%";

		System.out.println("Splitting String"
				+ " using colon as delimiter :\n");

		// create StringTokenizer object
		StringTokenizer st = new StringTokenizer(str, ":");

		// iterate through StringTokenizer tokens
		while(st.hasMoreTokens()) {
			System.out.println(st.nextToken());
		}
	}
}

Output

Splitting String using colon as delimiter :

100123
MotoG
4G-mobile
14520
10%

 

Points to be remember about StringTokenizer:

  • Although, we can split string using StringTokenizer class
  • But its usage is discouraged
  • As it is kept in the JDK only for the purpose of backward compatibility
  • So, if anyone intended to split a string then it is encouraged to use split() method of String class
  • As explained in the earlier articles various ways to split String and split() method of String class

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

 

Happy Coding !!
Happy Learning !!

Java: Count number of tokens using StringTokenizer
Convert first character of every word to uppercase in Java