See the Tutorial List

String tokenizer

In Java, String tokenizer is used to break a string in tokens. In string, tokenizer objects are maintained internally. It returns a token of a substring from the given string.

Example:

string-tokenizer

Following are the constructors in string tokenizer

1. StringTokenizer(String str)

2. StringTokenizer(String str, String delim)

3. StringTokenizer(String str, String delim, booleanreturnValue)

Following are the methods in string tokenizer

1. booleanhasMoreTokens()

2. String nextToken()

3. String nextToken(String delim)

4. booleanhasMoreElements()

5. Object nextElement()

6. intcountTokens()

Example:

	
import java.util.StringTokenizer;  
public class TokenDemo1
{  
	public static void main(String args[])
{  
	StringTokenizerobj = new StringTokenizer("Welcome to studytonight"," ");  
	while (obj.hasMoreTokens()) 
{  
		System.out.println(obj.nextToken());  
	}  
	}  
} 
	

Welcome to studytonight

	
import java.util.*;
public class TokenDemo2{
   public static void main(String args[])
     {
         String a= " : ";
         String b= "Welcome : to : studytonight : . : How : are : You : ?";
StringTokenizer c = new StringTokenizer(b, a);
int count1 = c.countTokens();
         for (inti = 0; i<count1; i++)
System.out.println("token [" + i + "] : "
                                + c.nextToken());
StringTokenizer d= null;
    while (c.hasMoreTokens())
System.out.println(d.nextToken());
     }
  }
	

string-tokenizer images