StringTokenizer |
|
/** 1. StringTokenizer The class StringTokenizer extends from java.util that extends from java.lang.Object. It emplements from Enumeration class. The main purpose of the StringTokenizer is to parse a set of string (as a sentence) into separated strings (tokens). The related main code is the following: StringTokenizer st = new StringTokenizer("All the best of luck"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } The constructors of the class StringTokenizer are: public StringTokenizer(String str) or public StringTokenizer(String str, String delimiter) or public StringTokenizer(String str, String delimiter, boolean returnDelimiters) The delimiter arguments are used to separate tokens. The delimiters characters are also returned as tokens if the returnDelims flag is true. Each delimiter is returned as a string of length one. If the flag is false, the delimiter characters are skipped and only serve as separators between tokens. A token consists of one or more characters of which none are blanks, control characters, or characters within a string constant or delimited identifier. Tokens are ordinary (like letters and numbers) or delimiter tokens (like operators and marks) 2. Example: */ import java.util.StringTokenizer; public class TheStringTokenizer { public static void main(String[] args) { //Create StringTokenizer fresh object StringTokenizer str = new StringTokenizer("Regards! and - All, The % best & of ? luck", "%", true); //Moving on through tokens by iteration while(str.hasMoreTokens()) System.out.println("\n\t" + str.nextToken("%")); } } /* Execution: ---------- C:\Java>javac TheStringTokenizer.java C:\Java>java TheStringTokenizer Regards! and - All, The % best & of ? luck */ |