Ads

Thursday, 2 November 2017

java.util.StringTokenizer class in java


StringTokenizer Class


The StringTokenizer class allows you to break the string into tokens.

It is available java.util package.

It is the simple way to break the string.

It doesn't provide the facility to differentiate numbers, quoted strings, identifiers etc. like StreamTokenizer class.


Constructors


StringTokenizer(String str)

This constructor is used creates StringTokenizer with the specified string.

StringTokenizer(String str, String delim)
This constructor constructs string tokenizer for the specified string.

 StringTokenizer(String str, String delim, boolean returnDelims)
This constructor constructs a string tokenizer for the specified string.


Methods


boolean hasMoreTokens()    This method is used to checks if there is more tokens available.

String nextToken()  

This method returns the next token from the StringTokenizer object.

String nextToken(String delim)    

This method returns the next token based on the delimiter.

boolean hasMoreElements()    

This method same as hasMoreTokens() method.

Object nextElement()    

This method same as nextToken() but its return type is Object.

int countTokens()    

This method returns the total number of tokens.



Example


import java.util.StringTokenizer;
public class Stringtokendemo

{
  public static void main(String args[])

  {
      StringTokenizer st = new StringTokenizer("This is      TheJavaXpert"," ");
      while (st.hasMoreTokens()) 

      {
         System.out.println(st.nextToken());
      }   

  }




 
Post a Comment

Click Here