[Java learning notes] use the split () method to split strings

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

// Use split () to extract substrings from a string. class splitdemo {static void showsplit (string [] STRs) {for (string STR: STRs) system. out. print (STR + "|"); system. out. println ("/N");} // demonstrate split (). public static void main (string ARGs []) {string result []; // split at spaces. string teststr = "this is a test. "; system. out. println ("original string:" + teststr); Result = teststr. split (" // S + "); system. out. print ("split at spaces:"); showsplit (result); // split on word boundaries. teststr = "one, two, and three. "; system. out. println ("original string:" + teststr); Result = teststr. split ("// W +"); system. out. print ("split at word boundaries:"); showsplit (result); // split same string on commas and zero or more spaces. system. out. println ("original string:" + teststr); Result = tests TR. split (", // s *"); system. out. print ("split at commas:"); showsplit (result); // split on word boundaries, but allow embedded // periods and @. teststr = "Jerry Jerry@HerbSchildt.com"; system. out. println ("original string:" + teststr); Result = teststr. split ("[// W & [^. @] + "); system. out. print ("allow. and @ to be part of a word: "); showsplit (result); // split on various punctuation and zero or m Ore trailing spaces. teststr = "this, is.! :; Test? "; System. Out. println (" original string: "+ teststr); Result = teststr. Split ("[.,!? :;] + // S * "); system. out. print ("split on various punctuation:"); showsplit (result) ;}} output: original string: this is a test. split at spaces: This | is | A | test. | original string: One, two, and three. split at word boundaries: One | two | and | three | original string: One, two, and three. split at commas: One | two | and three. | original string: The Jerry Jerry@HerbSchildt.comAllow. and @ to be part of a word: Jerry | Jerry @ H Erbschildt.com | original string: This, is.! :; Test? Split on various punctuation: This | is | A | test |

Author: gnuhpc

Source: http://www.cnblogs.com/gnuhpc/

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.