1. String to Array
The string in JS and the same in Java, are immutable constants. The early ECMAScript standard resolves string to an immutable array of characters, supporting access such as Str[index]. Firefox still supports it until today, but IE can't. I know two ways to convert a string to an array.
The first is easy to think of: using Charat.
JavaScript code:
function ToArray (str) {
if (typeof str!= "string") {
return [];
}
var arr=[];
for (Var i=0;i<str.length;i++) {
Arr.push (Str.charat (i))
}
return arr;
}
The second approach is extremely brief, but it is not easy to understand at a glance. But as long as one understanding, will certainly marvel at JS's wonderful.
JavaScript code:
var arr=str.match (/./g);
The Strobj.match (reg) method retrieves a string object and returns an array that contains all the matching results. The regular expression/./g matches all the characters, so Str.match (/./g) returns an array of all the characters in the string str to convert the strings to arrays.
2, array to string
Use the Strobj.join () method to specify the connector between the elements of an array, by default, "," As for Var arr=[' A ', ' B ', ' C '; Arr.join () returns "A,b,c", Arr.join ("") returns "ABC", Arr.join ("join") returns "Ajoinbjoinc".
After writing this article, I read the next book and found that Str.split ("") can also convert a string to a character array.
Example 1
In this case, we'll split the string in different ways:
<script type= "Text/javascript" >
var str= "How are your doing today?"
document.write (Str.split ("") + "<br/>")
document.write (Str.split ("") + "<br/>")
document.write (Str.split ("", 3))
</script>
Output:
How,are,you,doing,today?
H,o,w, A,r,e, Y,o,u, D,o,i,n,g, T,o,d,a,y,?
How,are,you
Example 2
In this case, we'll split the more complex string of structures:
"2:3:4:5". Split (":")//will return ["2", "3", "4", "5"]
"|a|b|c". Split ("|")//will return ["", "a", "B", "C"]
Example 3
Using the following code, you can split a sentence into words:
var words = Sentence.split (')
or use regular expressions as separator:
var words = Sentence.split (/\s+/)
Example 4
If you want to split the word into letters, or split the string into characters, use the following code:
"Hello". Split ("")//May return ["H", "E", "L", "L", "O"]
To return only a subset of the characters, use the Howmany parameter:
"Hello". Split ("", 3)//May return ["H", "E", "L"]
It was later found that join functions can combine strings into numbers
Example of a join method.
<script type= "Text/javascript" >
Examples of Join methods
var arr = new Array (3)
Arr[0] = "George"
ARR[1] = "John"
ARR[2] = "Thomas"
document.write (Arr.join ("."))
</script>
Output:
George.John.Thomas
The following is an introduction to the join and split methods for JS arrays.
1,join () and Split () method
<script type= "Text/javascript" >
Join Method Example
var x;
var a=new Array ();
A[0]= "XHTML";
A[1]= "CSS";
A[2]= "JavaScript";
Alert (A.join ());
Alert (typeof (A.join));
Alert (typeof (a));
</script>
The join () method is used to put all elements in an array into a string.
The elements are delimited by the specified delimiter.
Specifies the delimiter method join ("#"), where # can be any
The opposite is the split () method, which is used to split a string into an array of strings.