The java StringTokenizer is a class and it is used to break the String into pieces,called 'Tokens'.These Tokens are stored in the StringTokenizer object from where they are retrieved. To use StringTokenizer, you specify an input String and a String that contains delimiters. Here,delimiters are characters that separate the tokens,examples for delimiters ",.:"(comma,semicolon and colon). The default delimiters are white space characters like TAB,SPACE,new line and carriage return.
The StringTokenizer constructors are given below:
StringTokenizer(String str);
StringTokenizer(String str,String delimiters);
To create an object to StringTokenizer class is:
StringTokenizer str=new StringTokenizer(st,"delimiter");
In the above statement, the actual String st is broken into pieces at the positions marked by group of characters called delimiters.
Recommended to Read: String Handling in Java
For example,to break a string wherever comma is found then we write like
StringTokenizer str=new StringTokenizer(str,",");
To break the String wherever a comma or colon found or both are found,we can write:
StringTokenizer str=new StringTokenizer(str,",:");
StringTokenizer class methods:
StringTokenizer class includes the fallowing methods:
1. int intCounTokens();
This method counts and returns the number of tokens available in a StringTokenizer object.
2.boolean hasMoreTokens();
This method tests if there are more tokens available in the StringTokenizer object or not. If next token is there then it return true.
3.String nextToken():
This method returns the next token from the StringTokenizer object.
Example 1:
import java.util.*;
class STTest
{
public static void main(String args[])
{// take a string
String str="hi how are you";
//break into tokens at spaces,here delimiter is a space
StringTokenizer st=new StringTokenizer(str," ");
//retrieve tokens from st and disiplay
System.out.println("the tokens are:");
while(st.hasMoreTokens())
{
String one=st.nextToken();
System.out.println(one);
}
}
}
Output:
E:\javaprograms>javac STTest.java
E:\javaprograms>java STTest
the tokens are:
hi
how
are
you
Example 2:
import java.util.*;
class STTest {
static String in = "title=learnprogramingbyluckysir: corejava;" +
"author=byluckysir;";
public static void main(String args[]) {
StringTokenizer st = new StringTokenizer(in, "=;");
while(st.hasMoreTokens()) {
String key = st.nextToken();
String val = st.nextToken();
System.out.println(key + "\t" + val);
}
}
}
Output:
E:\javaprograms>javac STTest.java
E:\javaprograms>java STTest
title learnprogramingbyluckysir: corejava
author byluckysir
Read Also: Java String Interview Questions and answers
The StringTokenizer constructors are given below:
StringTokenizer(String str);
StringTokenizer(String str,String delimiters);
To create an object to StringTokenizer class is:
StringTokenizer str=new StringTokenizer(st,"delimiter");
In the above statement, the actual String st is broken into pieces at the positions marked by group of characters called delimiters.
Recommended to Read: String Handling in Java
For example,to break a string wherever comma is found then we write like
StringTokenizer str=new StringTokenizer(str,",");
To break the String wherever a comma or colon found or both are found,we can write:
StringTokenizer str=new StringTokenizer(str,",:");
StringTokenizer class methods:
StringTokenizer class includes the fallowing methods:
1. int intCounTokens();
This method counts and returns the number of tokens available in a StringTokenizer object.
2.boolean hasMoreTokens();
This method tests if there are more tokens available in the StringTokenizer object or not. If next token is there then it return true.
3.String nextToken():
This method returns the next token from the StringTokenizer object.
Example 1:
import java.util.*;
class STTest
{
public static void main(String args[])
{// take a string
String str="hi how are you";
//break into tokens at spaces,here delimiter is a space
StringTokenizer st=new StringTokenizer(str," ");
//retrieve tokens from st and disiplay
System.out.println("the tokens are:");
while(st.hasMoreTokens())
{
String one=st.nextToken();
System.out.println(one);
}
}
}
Output:
E:\javaprograms>javac STTest.java
E:\javaprograms>java STTest
the tokens are:
hi
how
are
you
Example 2:
import java.util.*;
class STTest {
static String in = "title=learnprogramingbyluckysir: corejava;" +
"author=byluckysir;";
public static void main(String args[]) {
StringTokenizer st = new StringTokenizer(in, "=;");
while(st.hasMoreTokens()) {
String key = st.nextToken();
String val = st.nextToken();
System.out.println(key + "\t" + val);
}
}
}
Output:
E:\javaprograms>javac STTest.java
E:\javaprograms>java STTest
title learnprogramingbyluckysir: corejava
author byluckysir
Read Also: Java String Interview Questions and answers
No comments:
Post a Comment