The use of Java Learning Note--stringtokenizer

Source: Internet
Author: User

Extension:Pattern.split replaces String.Split http://www.cnblogs.com/gnivor/p/4386978.html

StringTokenizer is an application class used to separate strings.

1. 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.  boolean returndelims): Constructs a StringTokenizer object to parse STR and provides a specified delimiter, specifying whether to return delimiters.

2. Introduction to Methods

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
Boolean hasmoreelements (): Returns whether there is a delimiter.
Boolean Hasmoretokens (): Ibid.
String NextToken (): returns the string from the current position to the next delimiter.
Object Nextelement (): same as the result, unless life returns an object instead of a string
String NextToken (String delim): Same as NextToken (), returns the result with the specified delimiter

Cases:

New String ("This was a test string"new"Token total:" + St.counttokens ());  while (St.hasmoreelements ()) {    System.out.println (st.nexttoken ());}

Example 2:

String str = "100|66,55:200|567,90:102|43,54"new stringtokenizer (str, ":, |"); // default does not print separators // stringtokenizer strtoke=new stringtokenizer (str, ":, |", true); // Print Separator // stringtokenizer strtoke=new stringtokenizer (str, ":, |", false); // do not print separators  while (Strtoke.hasmoretokens ()) {    System.out.println (Strtoke.nexttoken ());}

Note that the StringTokenizer delimiter does not need to use escape characters

3. The difference from split

String.Split () uses regular expressions, while StringTokenizer only uses characters that are split verbatim.
If you do not use a regular expression (StringTokenizer also does not work with regular expressions), StringTokenizer is the most efficient in intercepting strings. 】

The use of Java Learning Note--stringtokenizer

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.