The parsing method of the Java irregular string separated by commas (the field also contains commas)

Source: Internet
Author: User
Tags closing tag

/*** Java string comma split parsing method * This is specifically designed for cases where there are commas in double quotes or a field with a single quote * For example, string sss= "101,\" A\ "," China, Jiangsu \ "," b\ "," China,     BEIJING \ ", 1,0,\" c\ "" separated by commas to parse; * The correct split result is (101) (a) (China, Jiangsu) (b) (China, Beijing) (1) (0) (c) * If you use the Java split method, when you encounter (China, Beijing) These field values will be more divided a field out, is not correct, at the same time, the above 101, 1,0 without a double quote * We expect the ideal string to be a string of field values with double quotes, but we feel very annoyed when it happens. * Above is the original intention of this method design, in fact, this method is mentioned in the data structure of the university textbook, in this Java implementation of a bit , but the efficiency of the method execution I haven't tested yet * if anyone has a better way welcome to join the discussion O (∩_∩) o~ *@authorHsuchan *@version2014-11-30 22:30 *@paramSSS *@returnString []*/     Publicstring [] Commadivider (String sss) {//Double quote start tag        intQutationstart =0; //Double quote closing tag        intQutationend =0; Char[] Charstr =Sss.tochararray (); //used to stitch characters as a field valueStringBuffer SBF =NewStringBuffer (); //Results Listlist<string> list =NewArraylist<string>(); //CHAR-per- character processing         for(inti=0;i<charstr.length;i++) {            //no double quotes have been encountered before and the current character is \ "            if(Qutationstart = = 0&&charstr[i] = = ' \ ' ') {Qutationstart= 1; Qutationend= 0; Continue; } Else if(Qutationstart = = 1&&charstr[i] = = ' \ ') {//preceded by a double quotation mark and the current character is \ "Description field stitching that's overQutationstart= 0; Qutationend= 1; //when the last character is double quotation marks, because the next loop will not be executed, save it here                if(i = = Charstr.length-1&&sbf.length ()! = 0) {List.add (sbf.tostring ()); Sbf.setlength (0); }                Continue; } Else if(Qutationstart = = 1&&charstr[i] = = ', ' &&qutationend = = 0) {//handling \ "China, Beijing \" This nonstandard stringsbf.append (Charstr[i]); Continue; } Else if(Charstr[i] = = ', ') {//The field ends and the stitched field values are stored in the listList.add (sbf.tostring ()); Sbf.setlength (0); Continue; }            //do not belong to the delimiter on the splicingsbf.append (Charstr[i]); if(i = = Charstr.length-1&&sbf.length ()!=0) {List.add (sbf.tostring ()); Sbf.setlength (0); }                    }                return(string[]) List.toarray (Newstring[list.size ()]); }

The parsing method of the Java irregular string separated by commas (the field also contains commas)

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.