How to use JS split () function

Source: Internet
Author: User

Grammar

Stringobj.split ([separator[, limit]])

Parameters

Stringobj

Required option. The String object or text to be exploded. The object is not modified by the split method.

Separator

Options available. String or regular Expression object

Tips and comments

Note: If the empty string ("") is used as a, then each character in the Stringobject is split.
Note: The actions performed by String.Split () are the opposite of those performed by Array.join.


Cases

The code is as follows Copy Code

function Splitdemo () {
var s, SS;
var s = "The rain in Spain falls mainly in the plain.";
Decomposes at each space character.
SS = S.split ("");
return (SS);
}

If you want to use the character in a string, you also need to escape. For example, to First Express "AAAABBBB" the string should be "aaaa\bbbb", if you want to separate it should be so to get the correct result:

Cases

The code is as follows Copy Code

string[] aa = "AAA\BBB\BCCC". Split (\);

If you use the "." As a separate word, it must be written as follows: String.Split ("\.") so that the correct separation can be separated from the String.Split (".");


Cases

The code is as follows Copy Code

<script language= "JavaScript" >
function Spli () {
Datastr= "2,2,3,5,6,6";
var str= new Array ();

Str=datastr.split (",");
for (i=0;i<str.length; i++)
{
document.write (str[i]+ "<br/>");
}
}
Spli ();
</script>

Note that strs=str.split (","); After the character is split, the number will be an array.

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.