CodesJava

Easy learning with example program codes

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 codesjava
 */
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 codesjava
	 */
	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 codesjava
 */
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 codesjava
	 */
	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 codesjava
 */
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 codesjava
	 */
	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.

Related Topics:

StringBuffer in java.
append(String str) StringBuffer method in java.
insert(int offset, String str) StringBuffer method in java.
replace(int startIndex, int endIndex, String str) StringBuffer method in java.
delete(int startIndex, int endIndex) StringBuffer method in java.
reverse() StringBuffer method in java.
capacity() StringBuffer method in java.
ensureCapacity(int minCapacity) StringBuffer method in java.
StringTokenizer in java.

 

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap