StringTokenizer a preliminary understanding of string separators

Source: Internet
Author: User

Java.util.StringTokenizer, used to split a string.


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,
also specifies whether to return delimiters.

2, common methods.
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 (): Returns whether there is a next element.
4. String NextToken (): Returns the string from the current position to the next delimiter.
5. Object Nextelement (): Returns the object to the next delimiter.

6. String NextToken (String Delim): Returns the next delimiter at the current position.

Public class testofstringtokenizer {        public  static void main (String[] args)  {                     String str = new  String ("java   javascipt   c++ shell   plsql    python ")  ;           str = str  +  "\nhello\nworld"  ;          str =  str +  ", Kiss,my,ass"  ;          // Stringtokenizer st = new stringtokenizer (str, "    ", True);           stringtokenizer st = new stringtokenizer ( STR);      &NBsp;    system.out.println (  "token total: "  + st.counttokens ()  )           while ( st.hasmoreelements ()  ) {           system.out.println ( st.nextToken ()  );           }      }   }

Return Result: Token Total:8

Java javascipt C + + Shell Plsql PYTHON

Hello

World,kiss,my,ass


Counttokens: Returns the number of times the Nexttoken method was called.


The default constructor is used here.

The use of such a child does not return a delimiter.


Public class testofstringtokenizer {        public  static void main (String[] args)  {                     String str = new  String ("Tomorrow,i,will,go,to,school,alone")  ;            str = str +  ", Because,someone,want,to,kiss,my,ass"  ;           //Third parameter: Specifies whether to return a delimiter, the default is false         stringtokenizer st = new stringtokenizer (str, ",", true);           system.out.println (  "token total: "  +  St.counttokens ()  )           while (  St.hasmoreelements ()  ) {           system.out.println ( st.nexttoken ()  );           }      }  }

return Result: Token total:27

Tomorrow,i,will,go,to,school,alone,because,someone,want,to,kiss,my,ass (contains a comma total of 27 calls)

Counttokens: Returns the number of times the Nexttoken method was called.


This article is from the "beautiful Dē‖java Question" blog, please be sure to keep this source http://teny32.blog.51cto.com/8027509/1765697

StringTokenizer a preliminary understanding of string separators

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.