String splitting using JavaScript _ Javascript tutorial

Source: Internet
Author: User
JavaScript: use JavaScript to split strings. Javascript tutorial
The following is a reference clip:


String splitting

Function getmulti (oldstr ){
Var newstr = oldstr; // field content
Var I = 0, j = 0, t = 1; // determine whether there are multiple parts
Var foreindex; // record the position of the previous delimiter
Var index, depchar; // record the current delimiter and its location
Var linkstr = ""; // connection method
Var astr = new Array (10 );
Var index1 = newstr. indexOf (",");
Var index2 = newstr. indexOf (";");
Var index3 = newstr. indexOf ("% ");
Var index4 = newstr. indexOf (",");
Var index5 = newstr. indexOf (";");
Var index6 = newstr. indexOf ("");
Function LTrim (str ){
For (var x = 0; str. charAt (x) = ""; x ++ );
Return str. substring (x, str. length );
}
If (index1! =-1 ){
Index = index1;
Depchar = ",";
}
Else if (index2! =-1 ){
Index = index2;
Depchar = ";";
}
Else if (index3! =-1 ){
Index = index3;
Depchar = "% ";
}
Else if (index4! =-1 ){
Index = index4;
Depchar = ",";
}
Else if (index5! =-1 ){
Index = index5;
Depchar = ";";
}
Else if (index6! =-1 ){
Index = index6;
Depchar = "";
}
Else {
T = 0; index =-1;
Linkstr = "" + newstr + "";
}
Foreindex = 0;
While (index! =-1 ){
Index = newstr. indexOf (depchar, foreindex );
If (index = foreindex) {foreindex = index + 1; continue ;}
If (index! =-1 ){
Astr [j] = newstr. substring (foreindex, index );
Astr [j] = LTrim (astr [j]);
Foreindex = index + 1;
J = j + 1;
}

}
Astr [j] = newstr. substring (foreindex );
If (t = 1)
{
For (I = 0; I {
Linkstr = linkstr + "" + astr [I] + "";
}
}
Return linkstr;
}
Script



Var linkstring = "";
Var tmpkeyword = "Programmer software management software development software complexity software maintenance software model software standards ";
Linkstring = getmulti (tmpkeyword );
Document. write ("" + linkstring + "")
Script

SCRIPT function Preview (obj) {var TestWin = open (''); TestWin.doc ument. write (obj. value);} function copyCode (obj) {var rng = document. body. createTextRange (); rng. moveToElementText (obj); rng. scrollIntoView (); rng. select (); rng.exe cCommand ("Copy"); rng. collapse (false);} function saveCode (obj) {var winname = window. open ('', '_ blank', 'top = 10000 '); winname.doc ument. open ('text/html ', 'replace'); winname.doc ument. writeln (obj. value); winname.document.execcommand('saveas', '', 'homepage.yesky.com.htm '); winname. close ();} function addBookmark (title, url) {if (window. sidebar) {window. sidebar. addPanel (title, url, "");} else if (document. all) {window. external. addFavorite (url, title);} else if (window. opera & window. print) {return true;} SCRIPT<Html> <pead> <title> string segmentation </title> <script language = javascript> function getmulti (oldstr) {var newstr = oldstr; // field content var I = 0, j = 0, t = 1; // determine whether there are multiple parts of the content var foreindex; // record the position of the previous separator var index, depchar; // record the current delimiter and its location var linkstr = ""; // link mode var astr = new Array (10); var index1 = newstr. indexOf (","); var index2 = newstr. indexOf (";"); var index3 = newstr. indexOf ("%"); var index4 = newstr. indexOf (","); var index5 = n Ewstr. indexOf (";"); var index6 = newstr. indexOf (""); function LTrim (str) {for (var x = 0; str. charAt (x) = ""; x ++); return str. substring (x, str. length);} if (index1! =-1) {index = index1; depchar = ",";} else if (index2! =-1) {index = index2; depchar = ";";} else if (index3! =-1) {index = index3; depchar = "%";} else if (index4! =-1) {index = index4; depchar = ",";} else if (index5! =-1) {index = index5; depchar = ";";} else if (index6! =-1) {index = index6; depchar = "";} else {t = 0; index =-1; linkstr = "" + newstr + "";} foreindex = 0; while (index! =-1) {index = newstr. indexOf (depchar, foreindex); if (index = foreindex) {foreindex = index + 1; continue;} if (index! =-1) {astr [j] = newstr. substring (foreindex, index); astr [j] = LTrim (astr [j]); foreindex = index + 1; j = j + 1 ;}} astr [j] = newstr. substring (foreindex); if (t = 1) {for (I = 0; I <j + 1; I ++) {linkstr = linkstr + "" + astr [I] + "" ;}} return linkstr ;} script </pead> <body> <script language = javascript> var linkstring = ""; var tmpkeyword = "Programmer software management software development software complexity software maintenance software model software standards"; linkstring = getmulti (tmpkeyword); document. write ("" + linkstring + "") script </body> </ptml>
Run codeCopy codeSave codeAdd to favorites

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.