Use of the stringtokenizer class

Source: Internet
Author: User

Stringtokenizer is an application class used to separate strings. It is equivalent to the split function of VB.
1. Constructor
Public stringtokenizer (string Str)
Public stringtokenizer (string STR, string delim)
Public stringtokenizer (string STR, string delim, Boolean returndelims)
The first Delimiter is the string to be separated, the second Delimiter is the delimiter character set, and the third delimiter indicates whether the Delimiter is returned as a flag. If no Delimiter is specified, the default Delimiter is: "\ t \ n \ r \ f"
2. Core Methods
Public Boolean hasmoretokens ()
Public String nexttoken ()
Public String nexttoken (string delim)
Public int counttokens ()
In fact, there are three methods. When a Delimiter is returned, the delimiter can be specified. In the future, the Delimiter is the last delimiter specified by the delimiter.
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 stringtokenizer, and returning an object is meaningless.

Belongs to: Java. util package.

1. constructor.

1. stringtokenizer (string Str): constructs a stringtokenizer object used to parse Str. The default delimiter for Java is 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): constructs a stringtokenizer object for parsing STR, and provides a specified separator. At the same time, it specifies whether to return a separator.


2. method.
Note:
1. All methods are public;
2. Writing format: [modifier] <return type> <method name ([number of workers list])>
For example:
Static int parseint (string s) indicates that this method (parseint) is a class method (static), the return type is (INT), and the number of degrees required by the method is string.

1. Int counttokens (): the number of times the nexttoken method is called. Assume that delimiter uses constructor 1 and 2, and returns 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

 

 

From: http://blog.sina.com.cn/s/blog_8830b3ed0100y9m7.html

Use of the stringtokenizer class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.