Example of JavaScript string object split method (used to split strings into arrays), javascriptsplit

Source: Internet
Author: User

Example of JavaScript string object split method (used to split strings into arrays), javascriptsplit

JavaScript split method

The split method is used to split a string into a string array and return the array. The syntax is as follows:

Copy codeThe Code is as follows:
Str_object.split (separator, limit)

Parameter description:

Parameters Description
Str_object String (object) to be operated)
Separator Required. Separator, string or regular expression, which is used to separate str_object
Limit Optional. Specifies the maximum length of the returned array. If this parameter is set, no more substrings are returned than the array specified by this parameter. If this parameter is omitted, all compliant rules will be split.

Tip: If you use an empty string ("") as a separator, each character in str_object is separated, as shown in the following example.

Split Method Instance

Copy codeThe Code is as follows:
<Script language = "JavaScript">

Var str = "www.jb51.net ";
Document. write (str. split (".") + "<br/> ");
Document. write (str. split ("") + "<br/> ");
Document. write (str. split (".", 2 ));

</Script>

Run this example and output:

Copy codeThe Code is as follows:
Www, jb51, net
W,., j, B, 5, 1,., n, e, t
Www, jb51

Tip: As shown in the preceding example, if an empty string ("") is used as a separator, each character in str_object is separated.

The split method uses a regular expression.

The split method also supports the use of regular expressions to split strings:

Copy codeThe Code is as follows:
<Script language = "JavaScript">

Document. write ("1a2b3c". split (/\ d/) + "<br/> ");
Document. write (": a: B: c". split (":"));

</Script>

Run this example and output:

Copy codeThe Code is as follows:
A, B, c
, A, B, c

Observe the differences between the two examples.


In javascript, the use of split (a) is to split string objects by parameter a, and save the split string fragments in the array to return my question:

<Script language = "javascript">
Var datastr = "1a2a3a4a5 ";
Var str = new Array ();
Str = datastr. split ("");
For (var I = 0; I <str. length; I ++ ){
Document. write (str [I] + "<br/> ");
}
</Script>
This is the process you want and I hope it will help you!

Is the javascript split function that splits strings into arrays?

The split () method is used to split a string into a string array. Syntax stringObject. split (separator, howator) returns a string array. This array is created by dividing the string stringObject into substrings at the boundary specified by separator. The strings in the returned array do not include the separator itself. However, if separator is a regular expression that contains a subexpression, the returned array contains the strings that match the subexpression (but does not include the text that matches the entire regular expression ). Note: If the empty string ("") is used as a separator, each character in the stringObject is separated. Note: The operations executed by String. split () are the opposite to those executed by Array. join. Example:
"2: 3: 4: 5 ". split (":") // Returns ["2", "3", "4", "5"] "| a | B | c ". split ("|") // Returns ["", "a", "B", "c"]

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.