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/