StringTokenizer in Java


Prev Tutorial Next Tutorial

StringTokenizer in Java

It is a pre defined class in java.util package can be used to split the given string into tokens (parts) based on delimiters (any special symbols or spaces).

StringTokenizer injava

Suppose that we have any string like "Features of Java_Language" when we use stringTokenizer this string is split into tokens whenever spaces and special symbols present. After split string are :

Example

Features
of 
Java
Language

Methods of StringTokenizer

  • hasMoreTokens()
  • nextToken()

hasMoreTokens()

It is predefined method of StringTokenizer class used to check whether given StringTokenizer having any elements or not.

nextToken()

Which can be used to get the element from the StringTokenizer.

Example of StringTokenizer:

Example of StringTokenizer

import java.util.*;
class Stringtokenizerdemo
{
public static void main(String args[])
{
String str="He is a gentle man";
StringTokenizer st=new StringTokenizer(str," ");
System.out.println("The tokens are: ");
while(st.hasMoreTokens())
{
String one=st.nextToken();
System.out.println(one);
}
}
}

Output

The tokens are:
He
is
a
gentle
man

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements