Js splits strings into array instance summary using split

Source: Internet
Author: User

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]);
}

}

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.