Stringtokenizer: String-separated parsing Type
Belongs to: Java. util package.
1. constructor.
1. stringtokenizer (string Str): constructs a stringtokenizer object used to parse Str. Default Java delimiter
"Space", "tab ('\ t')", "line feed (' \ n')", and "Carriage Return ('\ R ')".
2. stringtokenizer (string STR, string delim): constructs a stringtokenizer object used to parse STR, and
Provides a specified separator.
3. stringtokenizer (string STR, string delim, Boolean returndelims): Construct
Stringtokenizer object, provides a specified separator, and specifies whether to return a separator.
2. method.
Note:
1. All methods are public;
2. Writing format: [modifier] <return type> <method name ([parameter list])>
For example:
Static int parseint (string s) indicates that this method (parseint) is a class method (static), and the return type is (INT)
The parameter required by the method is of the string type.
1. Int counttokens (): the number of times the nexttoken method is called. If constructors 1 and 2 are used
Is the number of separators (Example 2 ).
2. boolean hasmoretokens (): returns whether there are delimiters.
3. boolean hasmoreelements (): the result is the same as 2.
4. String nexttoken (): returns the string from the current position to the next separator.
5. Object nextelement (): the result is the same as 4.
6. String nexttoken (string delim): similar to 4, return results with the specified separator.
Example:
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 ());
}
Result:
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 ());
}
Result:
Token Total: 19
The
=
Java
=
Platform
=
Is
=
The
=
Ideal
=
Platform
=
For
=
Network
=
Computing