Copy Code code as follows:
<script language= "JavaScript" >
Str= "2,2,3,5,6,6"; This is a string
var strs= new Array (); Define an array
Strs=str.split (","); Character segmentation
for (i=0;i<strs.length; i++)
{
document.write (strs[i]+ "<br/>"); Character output after segmentation
}
</script>
The use of split in JS
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>split</title>
<script type= "Text/javascript" >
var array;
function getString (str) {
Array = str.split ("|");
document.getElementById ("UserName"). Value = Array[0];
document.getElementById ("Userage"). Value = Array[1];
}
</script>
<body onload= "getString (' Zhou |22 ')" >
<input type= "text" id= "UserName" name= "UserName"/><br/><br-/>
<input type= "text" id= "Userage" name= "Userage"/>
</body>
JS Delete array elements
var arr=[' A ', ' B ', ' C '];
There are two ways to delete a ' B ' in it:
1.delete method: Delete Arr[1]
This way the array length does not change, at this time arr[1] becomes undefined, but also has the advantage the index of the original array also remains unchanged, at this time to enumerate the elements of the group can be used
For (index in ARR)
document.write (' arr[' +index+ ']= ' +arr[index]);
This traversal way skips over the elements of the undefined
* This way IE4.O after the support of the
2. Array Object Splice method: Arr.splice (1,1);
This way the array length changes, but the original array index changes accordingly
The first 1 in the splice parameter is the starting index of the deletion (starting from 0), which is the second element of the array
The second 1 is to delete the number of elements, where only one element is deleted, i.e. ' B ';
Traversing an array element at this time can be used to traverse the array in a normal way, for example, because the deleted element
The array does not hold
* This method IE5.5 later support
It is worth mentioning that the splice method can be added to an array element while deleting an array element
For example Arr.splice (1,1, ' d ', ' e '), d,e two elements are added to the array arr
The resulting array becomes arr: ' A ', ' d ', ' e ', ' C '