StringTokenizer

StringTokenizer is used specify input string,that string contains spaces,comma,dot,semicolon etc......these are called delimeters,the StringTokenizer implements the Enumeration interface.there are some methods to work with StringTokenizer listed bellow

  • nextToken() is used to extract the consecutive tokens.
  • hasMoreTokens() it returns true while there are more tokens to be extracted
  • hasMoreElements(),nextElement() methods works same as the hasMoreToken() because StringTokenizer implements Enumeration
Example
 
import java.util.*;
 
public class StTokenizer {
public static void main(String[] args) {
 
String str = "Hi ,This is ,StringTokenizer, programe.Have, great ,day......";
StringTokenizer st = new StringTokenizer(str);
 
System.out.println("---- Split by space ------");
while (st.hasMoreElements()) {
System.out.println(st.nextElement());
}
 
System.out.println("---- Split by comma ',' ------");
StringTokenizer st2 = new StringTokenizer(str, ",");
 
while (st2.hasMoreElements()) {
System.out.println(st2.nextElement());
}

}
}

No comments:

Post a Comment