Saturday, 4 February 2017

What is StringTokenizer Class In Java ?

StringTokenizer Class 

All Implemented Interfaces:
Enumeration<Object>

Declaration of StringTokenizer:
public class StringTokenizer
extends Object
implements Enumeration<Object>

This class belongs to java.util; package. StringTokenizer class is used to break String into tokens. This class uses delimiters to break String, delimiters are characters that separate the String into tokens.

Delimiters can be specified at the creation time or on each token.
StringTokenizer returns token by taking a substring of the String that was used to create String Tokenizer object.

Constructor Summary of StringTokenizer:

Constructor
Description
StringTokenizer(String str)
Constructs a string tokenizer for the specified string.
StringTokenizer(String str, String delim)
Constructs a string tokenizer for the specified string.
StringTokenizer(String str, String delim, boolean returnDelims)
Constructs a string tokenizer for the specified string.

Method Summary of StringTokenizer:

Modifier and Method Name
Description
int countTokens()
Calculates the number of times that this tokenizer's  next Token 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.
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 from this string tokenizer.
String nextToken(String delim)
Returns the next token in this string tokenizer's string.

Example of StringTokenizer:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.StringTokenizer;

public class StringTokenizerClassExample {
 
 public static void main(String[] args) {
  
  //StringTokenizer with String in Constructor(default constructor)
  System.out.println("OUTPUT 1 :\n----------");
  StringTokenizer st = new StringTokenizer("Java Supports OOPS features");
  while(st.hasMoreTokens()){
   System.out.println(st.nextToken());
  }
  
  //StringTokenizer with String in Constructor & delimeter
  System.out.println("\nOUTPUT 2 :\n----------");
  StringTokenizer st1 = new StringTokenizer("I_love_Java","_");
  while(st1.hasMoreTokens()){
   System.out.println(st1.nextToken());
  }
  
  //StringTokenizer with String in Constructor ,delimeter & boolean flag 
  System.out.println("\nOUTPUT 3 :\n----------");
  StringTokenizer st2 = new StringTokenizer("Java,Identifiers",",",false);
  while(st2.hasMoreTokens()){
   System.out.println(st2.nextToken());
  }
  
  StringTokenizer st3 = new StringTokenizer("Java,Supports,8,primitive,data,types",",");
  //Calculates the number of times that this tokenizer's  next Token method can be called before it generates an exception
  System.out.println("\nTotal Tokens : "+st3.countTokens());
  
  //Tests if there are more tokens available from this tokenizer's string.
  System.out.println("hasMoreTokens() : "+st3.hasMoreTokens());
    
  //Returns the same value as the hasMoreTokens method
  System.out.println("hasMoreElements() : "+st3.hasMoreElements());
    
  //Returns the next token from this string tokenizer.
  System.out.println("nextToken() : "+st3.nextToken());
  
  //Returns the same value as the nextToken method, except that its declared return value is Object rather than String.
  System.out.println("nextElement() : "+st3.nextElement());
 
  //Returns the next token in this string tokenizer's string
  System.out.println("nextToken(String delim) : "+st3.nextToken(","));
    
 }
}

Output:

OUTPUT 1 :
----------
Java
Supports
OOPS
features

OUTPUT 2 :
----------
I
love
Java

OUTPUT 3 :
----------
Java
Identifiers

Total Tokens : 6
hasMoreTokens() : true
hasMoreElements() : true
nextToken() : Java
nextElement() : Supports
nextToken(String delim) : 8


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment