The following code has a good idea without considering the running efficiency. But in actual applications, we still use split or join to implement Array Operations. Two custom js functions that implement mutual conversion between strings and arrays hope to be useful to you:
The Code is as follows:
Function StringToArray (str, substr ){
/* Function: Splits a string into an Array Based on the specified string.
Parameters:
Str: string to be converted
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 (I 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: convert an array to a string based on the delimiter (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 more information about the application, see the related articles of the home of the foot.
Javascript array summary using call Methods
Http://www.jb51.net/article/13084.htm
Practical Tips for javascript Array Operations
Http://www.jb51.net/article/19987.htm