JS Split split String function using the detailed

Source: Internet
Author: User
Tags regular expression

Definitions and usage

The split () method is used to split a string into an array of strings.

Grammar

Stringobject.split (Separator,howmany)

Parameter description

Separator required. A string or regular expression that splits the Stringobject from the place specified by the parameter.
Howmany Optional. This parameter specifies the maximum length of the array to return. If this argument is set, the returned substring will not be more than the array specified by this parameter. If this argument is not set, the entire string is split, regardless of its length.

Cases

The code is as follows Copy Code

<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 output result is
2
2
3
5
6
6

An instance of your own application

  code is as follows copy code

function Checkarbitration (data) {

var stars = new Array ()

Stars = Data.split (",");

Switch (stars[1]) {

case ' 1 ': {

return ';

Break;}

Case ' 2 ': {

return ';

Break;}

Case ' 3 ': {

return ';

Break;}

Case ' 4 ': {

if (stars[2] = = 1) {

return ' <a href= ' javascript:void (0); ' onclick= ' applyarbitration ("+ stars[0]+ ") ' > I have paid, but the seller has not confirmed for a long time </a>";

} else {

return ';

}

break;}

Case ' 5 ': {

return ';

Break;}

Case ' 6 ': {

return ';

Break;}

Case ' 7 ': {

return ';

Break;}

}

}


JS has a string of var str = ' 1|2|3|4|5|6#1|2|3|4#1|2|3|4|5|6|7|8 '; Please split it into an array with the # number.
then use the "|" Split it into a 2-D array.
You can remove each element of an array with a variety of such things as ary[0][0.
The last output is the number of the longest array (the natural number 1,2,3), the number of elements in the array. And each element in a large array.

The code is as follows Copy Code

<meta http-equiv= "Content-type" content= "text/html; CHARSET=GBK "/>
<script>
function GetResult ()
{
var yiwei=new Array ();
var brray=new Array ();
var i=0;
var n=0;
var m=0;
var Str=document.getelementbyid ("Input1"). Value;
Yiwei=str.split ("#");
for (i=0;i<yiwei.length;i++) {
Brray[i]=yiwei[i].split ("|");
if (brray[i].length>n) {
N=brray[i].length;
m=i+1;
}
}
var str1= "The longest array is" +m+ "<br>" + "Length" +n+ "<br>";

for (Var i=0;i<brray.length;i++) {
Str1+=brray[i].join (",") + "<br/>";
}
document.getElementById ("Result"). Innerhtml=str1;
}
</script>
<body>
<pre>
JS has a string of var str = ' 1|2|3|4|5|6#1|2|3|4#1|2|3|4|5|6|7|8 '; Please split it into an array with the "#" number,
Then use the "|" Split it into a 2-D array.
You can remove each element of an array with a variety of such things as ary[0][0.
The last output is the number of the longest array (the natural number 1,2,3), the number of elements in the array. And each element in a large array.
</pre>
<input type= "text" id= "input1" value= "1|2|3|4|5|6#1|2|3|4#1|2|3|4|5|6|7|8" ><input type= "button" onclick= " GetResult () "value=" for results ><br/>
Results
<div id= "Result" >
</div>
</body>

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.