the use of StringTokenizer learning in Java
The string class in Java can decompose strings and use the substring method to return substrings of the original string. If we need to break the string into a single word or tag, we can use the StringTokenizer class in Java at this point.
There are two common methods of StringTokenizer:
1.hasMoreElements (). This method is the same as the use of the hasMoreElements () method, except that the method implemented by StringTokenizer to implement the enumeration interface can be seen from the StringTokenizer declaration: public class StringTokenizer implements Enumeration<object>.
2.nextElement (). This method is the same as the Nexttoken () method, and returns the next token for this stringtokenizer.
Let's take a look at its constructor: three cases
1: The default is "\t\n\r\f" (there is a space before, the quotation marks are not) for the delimiter.
Public StringTokenizer (String str) {
This (str, "\t\n\r\f", false);
}
2:public StringTokenizer (String str, string delim) {
This (str, delim, false);
}
3: public StringTokenizer (String str, String Delim, Boolean returndelims). Returndelims is true, the Delim delimiter is also considered a marker.
Here are two examples:
One: string s = new String ("The Java platform is the ideal platform for Network Computing");
StringTokenizer st = new StringTokenizer (s);
System.out.println ("Token total:" + st.counttokens ());
while (St.hasmoreelements ()) {
System.out.println (St.nexttoken ());
}
The output is:
Token total:10
The
Java
Platform
Is
The
Ideal
Platform
For
Network
Computing
Two: String str = new String ("the=java=platform=is=the=ideal=platform=for=network=computing");
StringTokenizer Stz = new StringTokenizer (str, "=", true);//flag indicating whether to return the delimiters as tokens
System.out.println ("Token total:" + stz.counttokens ());
while (Stz.hasmoreelements ()) {
System.out.println (Stz.nextelement ());
}
The output is:
Token total:19
The
=
Java
=
Platform
=
Is
=
The
=
Ideal
=
Platform
=
For
=
Network
=
Computing
The use of StringTokenizer learning in Java