Pages

String Tokenizer in java

What is String Tokenizer in java?
  • StringTokenizer class is used to break string into tokens.
  • StringTokenizer class is available in java. util package
  • StringTokenizer("string") - It will create string tokenizer with the specified string
  • StringTokenizer("string","delimiter") - It will break the string into tokens based on delimiter
  • StringTokenizer("string","delimiter",boolean) - It will break the string into tokens based on delimiter and returns delimiter value based upon boolean value. If true delimiter value will take otherwise it won't.
import java.util.StringTokenizer;

public class stringTokenizer {
public static void main(String args[]) {
StringTokenizer st = new StringTokenizer("This is my first string");
while (st.hasMoreElements()) {
System.out.println(st.nextToken());
}

System.out.println("----StringTokenizer using with delimiter");
System.out.println("------------------------------");
StringTokenizer st1 = new StringTokenizer("stringtokenizer with$ delimiter", "$");
while (st1.hasMoreTokens()) {
System.out.println(st1.nextToken());
}

System.out.println("----StringTokenizer using with delimiter and delimiter return value");
System.out.println("----------------------------");
StringTokenizer st2 = new StringTokenizer("stringtokenizer with$ delimiter", "$", true);
while (st2.hasMoreTokens()) {
System.out.println(st2.nextElement());
}
}
}
Output:
string tokenizer program in java

Note: string tokenizer is deprecated and it was preferred to use split() method or you can use Regular expressions.
Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.