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!!!

Source: Internet
Author: User



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!!!


Related Article

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.