Detailed usage of the split () function in JavaScript

Source: Internet
Author: User
Tags split words
The JavaScriptsplit () method JavaScriptString Object Reference Manual defines and uses the split () method to split a string into a string array. The parameter description of the stringObject. split (separator, howator) is required. String or regular expression, which separates the stringObject JavaScript split () method from the place specified by this parameter

JavaScript String object reference manual


Definition and usage

The split () method is used to split a string into a string array.


Syntax
StringObject. split (separator, howator)

Parameters Description
Separator Required. String or regular expression, which separates stringObject from the specified place.
Howmany 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.

Return Value

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 ).


Tips and comments

NOTE: If an 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.


Instance
Example 1

In this example, we will split the string in different ways:




Var str = "How are you doing today? "

Document. write (str. split ("") +" ")
Document. write (str. split ("") +" ")
Document. write (str. split ("", 3 ))

Script

Output:


How, are, you, doing, today?
H, o, w, a, r, e, y, o, u, d, o, I, n, g, t, o, d,, y ,?
How, are, you


Example 2

In this example, we split strings with more complex structures:


"2: 3: 4: 5". split (":") // Returns ["2", "3", "4", "5"]
"| A | B | c". split ("|") // ["", "a", "B", "c"]


Example 3

Use the following code to split sentences into words:


Var words = sentence. split ('')

Or use the regular expression as the separator:


Var words = sentence. split (/\ s + /)


Example 4

If you want to split words into letters or strings into characters, use the following code:


"Hello". split ("") // you can return ["h", "e", "l", "l", "o"]

If you only need to return a part of the characters, use the howmany parameter:


"Hello". split ("", 3) // you can return ["h", "e", "l"]


TIY
Split ()
How to use split () to separate strings.

JavaScript String object reference manual


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.