Split string Split function in JavaScript

Source: Internet
Author: User

Suppose the string to be split is: s= "... fs...fs ..." where FS represents the character or string to be delimited.

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

Grammar
Stringobject.split (Separator,howmany)

Instance 1

The code is as follows Copy Code

var ss=s.split ("FS");

for (Var i=0;i<ss.length;i++) {

Handle each ss[i];

}

Instance 2

In this case, we'll split the more complex string of structures:

The code is as follows Copy Code

"2:3:4:5". Split (":")//will return ["2", "3", "4", "5"]
"|a|b|c". Split ("|")//will return ["", "a", "B", "C"]


Instance 3

The code is as follows Copy Code

<script type= "Text/javascript" >
var str = "1234";
var str1 = "Basketball, volleyball, table tennis";
var arr = str.split ("");//All Split
var arr1 = Str1.split (",");//split by comma
var arr2 = Str1.split (",", 2);//divide by comma, keep two paragraphs
</script>

We can test it out locally.


Instance 3

The code is as follows Copy Code


<input id= "x" type= "text"/>
<input type= "button" onclick= "x ()" Value= "Enter mail address, get username"/>
<script>
function X () {
var X=document.getelementbyid ("X"). value.tostring ();
var c=x.split ("@");
document.getElementById ("x"). Value=c[0];
}
</script>

And we'll come up with a VBScript approach.

Below in ASP VBScript:

The code is as follows Copy Code

Dim ss

Ss=split (S, "FS")

For I=lbound (ss) To UBound (ss)

Process each SS (i)

Next

Note: LBound (ss) should be 0, if split fails, UBound return-1


Note: If an empty string ("") is used as a separator, then each character in the Stringobject is split.

Summary: The split function is much like what we used to do in PHP and the ASP character segmentation function, it as long as what the split line can be the content we want to split the group.

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.