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"]