JS implements a string cut and converts it into an object format saved to the local

Source: Internet
Author: User

//split () splits the string into a string array according to the specified rules, and returns the array (method of string to array) //Split String    varBSTR = "Www.baidu.con"; vararr1 = Bstr.split (");//do not specify any separators, each of them will be splitConsole.log (ARR1);//W "," W "," W ",". "," B "," A "," I "," D "," U ",". "," C "," O "," n "    varARR2 = Bstr.split (".");//use the "." As separatorsConsole.log (ARR2);//["www", "Baidu", "con"]    varARR3 = Bstr.split ('. ', 2);//use the "." As a delimiter, and specifies the maximum length of the returned arrayConsole.log (ARR3);//["www", "Baidu"]  //Complex String Segmentation: Extracts the names and ages of the following three people, generating an array of objects    varDatastr = "Editorialcommittee,29& Chu Yaven,32& Baiyu, 28"; //first split: separate three-person information    varPeoplearr = Datastr.split (' & '));    Console.log (Peoplearr); varObjarr = []; //iterate through the resulting array     for(IinchPeoplearr) {        //Console.log (Peoplearr[i]);        //splits the information for each index in the number again and declares an array variable to receive        varPeopledata = Peoplearr[i].tostring (). Split (', '); //Console.log ("Peopledata:" +peopledata);        //remove data from an array and store it in an object        varobj ={name:peopledata[0], age:peopledata[1]        }        //console.log ("obj:", obj);        //to store an object in an array of objectsObjarr.push (obj);    } console.log (Objarr); //saves the data as a string in a local    varObjstr =json.stringify (Objarr); Localstorage.plpdata= Objstr;//localstorage data storage with no time limit

JS implements a string cut and converts it into an object format saved to the local

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.