If you have learned asp, you will not feel that split is a stranger. It divides strings into arrays using our specified delimiters. The same is true for string splitting in js, let's take a look at several instances.
Definition and usage
The split () method is used to split a string into a string array.
Syntax
StringObject. split (separator, howator)
Parameter description
Separator is required. String or regular expression, which separates stringObject from the specified place.
Optional. This parameter 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 not set, the entire string is split, regardless of its length.
Example
Split strings into arrays using split
The Code is as follows: |
Copy code |
Birthday = '2017-12-16 '; Arr = birthday. split ("-"); // use-to split a date Arr [0] // 1990 Arr [1] // 12 Arr [2] // 16 |
Example
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 (); // defines an Array Strs = str. split (","); // delimiter For (I = 0; I <strs. length; I ++) { Document. write (strs [I] + "<br/>"); // delimiter output } </Script> |
Examples in my project:
The Code is as follows: |
Copy code |
Function SelectCompany () { Var str = window. showModalDialog ("MessageFrame.htm", "", "dialogWidth = 730px, dialogLeft = 400px, dialogTop = 400px; center: yes; status: no ");
Var str2 = new Array (); Str2 = str. split ("| "); Var list1 = document. getElementById ("lstSendObjects "); For (I = 0; I <str2.length; I ++) { List1.add (new Option (str2 [I], str2 [I]); }
} |