Javascript string and array conversion functions [do not use split and join]

Source: Internet
Author: User
Tags javascript array

Two custom JS functions that implement mutual conversion between strings and arrays hope to be useful to you:

CopyCode 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 <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: 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; I <arr. length; I ++ ){
If (ARR [I]! = ""){
If (strtmp = ""){
Strtmp = arr [I];
} Else {
Strtmp = strtmp + STR + arr [I];
}
}
}
Return strtmp;
}

For specific applications, refer to the related topic home.Article.
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

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.