StringTokenizer is an application class that separates strings, which is equivalent to the Split function of VB.
1. Constructors
Public StringTokenizer (String str)
Public StringTokenizer (String str, string Delim)
Public StringTokenizer (String str, String Delim, Boolean returndelims)
The first parameter is the string to be delimited, the second is the delimited character set, and the third parameter indicates whether the delimiter is returned as a token, assuming that no separator character is specified, and the default is: "\t\n\r\f"
2. Core approach
public boolean Hasmoretokens ()
Public String NextToken ()
public string NextToken (string Delim)
public int Counttokens ()
In fact, there are three methods, which can be used to specify the converts sequential blocks when the delimiter is returned, and the last delimiter specified.
3. Redundant methods
public boolean hasmoreelements ()
public boolean hasmoreelements ()
This class implements the enumeration interface, so there are two more methods, in fact there is no need to implement this interface
Its name is called StringTokenizer, and returning an object has no meaning.
Belongs to: Java.util package.
1, the constructor function.
1. StringTokenizer (String str): Constructs a StringTokenizer object to parse str. The default Java delimiter is "space", "tab (' \ t ')", "line feed (' \ n ')", "carriage return (' \ R ')".
2. StringTokenizer (String str, string Delim): Constructs a StringTokenizer object to parse STR and provides a specified delimiter.
3. StringTokenizer (String str, String Delim, Boolean returndelims): Constructs a StringTokenizer object to parse STR and provides a specified delimiter, at the same time, Specifies whether to return delimiters.
2, methods.
Description
1. All methods are public;
2. Writing format: [modifier] < return type >< method name ([list of references]) >
Such as:
The static int parseint (String s) indicates that this method (parseint) is a class method (static) and the return type is (int), and the method requires a String type.
1. Int Counttokens (): Returns the number of times the Nexttoken method was called. Assuming that constructors 1 and 2 are used, the number of separators is returned (example 2).
2. Boolean hasmoretokens (): Returns whether there is a delimiter.
3. Boolean hasmoreelements (): The result is the same as 2.
4. String NextToken (): Returns the string from the current position to the next delimiter.
5. Object nextelement (): The result is the same as 4.
6. String NextToken (String Delim): Similar to 4, returns the result with the specified delimiter.
Examples:
Code:
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 result is:
Token total:10
The
Java
Platform
Is
The
Ideal
Platform
For
Network
Computing
Example 2:
Code:
string s = new String ("the=java=platform=is=the=ideal=platform=for=network=computing");
StringTokenizer st = new StringTokenizer (s, "=", true);
System.out.println ("Token total:" + st.counttokens ());
while (St.hasmoreelements ()) {
System.out.println (St.nexttoken ());
}
The result is:
Token total:19
The
=
Java
=
Platform
=
Is
=
The
=
Ideal
=
Platform
=
For
=
Network
=
Computing
Source: http://blog.sina.com.cn/s/blog_8830b3ed0100y9m7.html
Use of the StringTokenizer class