JavaScript string and array conversion functions [without split and join]_javascript tips

Source: Internet
Author: User

Two custom implementations of the string and the array of conversion of the JS function, hope to be useful to everyone:

Copy Code code as follows:

function StringToArray (STR,SUBSTR) {
/* Function function: strings are converted to arrays according to the specified string partition
Parameters:
STR: string to convert
SUBSTR: Split string
return value:
The converted Array
*/
var arrtmp = new Array ();
if (substr== "") {
Arrtmp.push (str);
return arrtmp;
}
var i=0, j=0, k=str.length;
while (i<k) {
j = Str.indexof (substr,i);
if (j!=-1) {
if (str.substring (i,j)!= "") {Arrtmp.push (str.substring (i,j));
i = j+1;
} else {
if (str.substring (i,k)!= "") {Arrtmp.push (str.substring (i,k));
i = k;
}
}
return arrtmp;
}

function arraytostring (ARR,STR) {
/* Function function: Array converted to string based on split character (string)
Parameters:
Arr: Array of strings to convert
STR: Split string
return value:
Converted String
*/
var strtmp = "";
for (Var i=0;i<arr.length;i++) {
if (arr[i]!= "") {
if (strtmp== "") {
strtmp = Arr[i];
} else {
strtmp = strtmp + str + arr[i];
}
}
}
return strtmp;
}

Specific applications can refer to the relevant articles in the cloud-dwelling community.
The JavaScript array uses the Call method rollup
Http://www.jb51.net/article/13084.htm

Practical tips for JavaScript array manipulation
Http://www.jb51.net/article/19987.htm

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.