Split method: splits a string into substrings and returns the result as an array of strings.
Stringobj.split ([Separator],[limit])
Parameters: Stringobj required option. the String object or text to be decomposed . The object is not modified by the split method.
Separator options available. A string or regular expression object that identifies whether one or more characters are used when separating the string. If you suddenly
This option returns a single-element array that contains the entire string. The limit option is available. This value is used to limit the number of elements in the returned array.
Description: The result of the split method is an array of strings, which are decomposed for each occurrence of separator in Stingobj . Separator is not returned as part of any array element.
Split's implementation directly calls the split method of the Matcher class. " . " There is a special meaning in the regular expression, so we must escape "\ \" when we use it.
If you use the vertical bar "|" Separated, there will be an unreachable result, which must be replaced by "\\|"
, + * is not a valid pattern-matching rule expression, and the correct result is obtained after escaping with "//*" "//+ ".
Instance:
Public classStrSplit2 { Public Static voidMain (string[] args) {String orgstr= "A,b,c,d,e,f"; String[] RESULT1= Orgstr.split (","); String[] Result2= Orgstr.split (",", 4); for(intA = 0; A < Result1.length; a++) {System.out.print (Result1[a]+ "\ T"); } System.out.println (); for(intA = 0; A < Result2.length; a++) {System.out.print (Result2[a]+ "\ T"); } System.out.println (); string[] AA= "AAA|BBB|CCC". Split ("\\|");//so we can get the right results. for(inti = 0; i < aa.length; i++) {System.out.println (aa[i]); } System.out.println (); string[] BB= "AAAA*BBBB*". Split ("\\*");//so we can get the right results. for(inti = 0; i < bb.length; i++) {System.out.println (bb[i]); } string[] CC= "Acount=?" and UU =? or n=? ". Split ("And|or");//so we can get the right results. for(inti = 0; i < cc.length; i++) {System.out.println (cc[i]); } string[] DD= "AAA\\BBB\\BCCC". Split ("\\\\")); for(inti = 0; i < dd.length; i++) {System.out.println (dd[i]); } System.out.println (); String s=NewString ("01: Big Car"); String a[]= S.split (":"); System.out.println (a[0]); System.out.println (a[1]); System.out.println (); String Str= "1ONE123TWO456OBC"; String[] Strs2= Str.split ("o"); for(inti = 0; i < strs2.length; i++) System.out.println ("strs2[" + i + "] =" +Strs2[i]); }}
Java face question 17 How do I convert a comma-delimited string into an array? About the use of the split method in the String class, Super detailed!!!