Stringtokenizer class (string processing)

Source: Internet
Author: User

Analyze the string and break it into words that can be used independently. These words are called language symbols. For example, for the string "We are struts", if spaces are used as the separator for this character

The string contains three words (language symbols ).
Constructor:
A) stringtokenizer (string s) -- constructs an analyzer for string S and uses the default delimiter, including space, line feed, carriage return, and tab.
B) stringtokenizer (string S, string delim) -- constructs an analyzer for string S. Any permutation and combination of Characters in the parameter 'delimiter' is a separator.
Hasmoretokens () method returns true if the value of the variable to be counted is greater than 0. Otherwise, false is returned.

Stringtokenizer Fenxi = new stringtokenizer ("99999.9999 ",".");
For (int I; Fenxi. hasmoretokens (); I ++ {
String STR = Fenxi. nexttoken ();
System. Out. println (STR );
}
Returns a token by intercepting a substring of a string, which is used to create a stringtokenizer object.

Code:
  1. // The following is an instance using tokenizer. The Code is as follows:
  2. Stringtokenizer ST = new stringtokenizer ("this is a test ");
  3. While (St. hasmoretokens ()){
  4. System. Out. println (St. nexttoken ());
  5. }
  6. // Output the following string:
  7. This
  8. Is
  9. A
  10. Test
  11. // Stringtokenizer is a legacy class reserved for compatibility reasons (although it is not encouraged in new code ). We recommend that you use the string split method or
  12. Java. util. RegEx package.
  13. // The following example illustrates how to use the string. split method to break the string into basic tags:
  14. String [] result = "this is a test". Split ("// s ");
  15. For (INT x = 0; x <result. length; X ++)
  16. System. Out. println (result [x]);
  17. /* Output the following string:
  18. This
  19. Is
  20. A
  21. Test */

 

 

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.