Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 18:39:29
Original
1601 people have browsed it

Two custom js functions for converting strings and arrays, I hope they can be useful to everyone:

Copy code The code is as follows:

function StringToArray(str,substr) {
/* Function function: The string is divided and converted into an array according to the specified string
Parameters:
str: required Converted string
substr: Split string
Return value:
Converted array
*/
var arrTmp = new Array();
if(substr==" ") {
arrTmp.push(str);
return arrTmp;
}
var i=0, j=0, k=str.length;
while(ij = 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: Convert array to string according to splitting characters (string)
Parameters:
arr: String array to be converted
str: Split string
Return value:
Converted string
*/
var strTmp = "";
for(var i=0;iif(arr[i]!=" ") {
if(strTmp=="") {
strTmp = arr[i];
} else {
strTmp = strTmp str arr[i];
}
}
}
return strTmp;
}

For specific applications, please refer to the relevant articles of Script House.
Javascript array usage call method summary
http://www.jb51.net/article/13084.htm

Practical tips for javascript array operations
http://www.jb51.net/article/19987.htm

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!