"Reprint" The Role of the StringTokenizer class in Java

Source: Internet
Author: User

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, and if no separator character is specified, 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, you can also specify the delimiter when you return the separator character converts sequential blocks, and you will use the last specified separator symbol.
3. Redundant methods
Public Boolean hasmoreelements ()
Public Boolean hasmoreelements ()
This class implements the enumeration interface, so there are two more methods, but there is no need to implement this interface at all .
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 ([parameter list]) >
Such as:
The static int parseint (String s) represents: This method (parseint) is a class method (static), the return type is (int), and the method requires a parameter of type String.

1. Int Counttokens (): Returns the number of times the Nexttoken method was called. If 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.


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 ());
}
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

"Reprint" The Role of the StringTokenizer class in Java

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.