Summary of the use of split () in JavaScript, javascriptsplit

Source: Internet
Author: User
Tags split words

Summary of the use of split () in JavaScript, javascriptsplit

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

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) // output: // How, are, you, doing, today? // H, o, w, a, r, e, y, o, u, d, o, I, n, g, t, o, d, a, 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 ("|") // Returns ["", "a", "B", "c"]

Example 3

Use the following code to split sentences into words:

Var words = sentence. split ('') // or use a regular expression as 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 ("") // Returns ["h", "e", "l", "l", "o"] // If you only need to return a part of the characters, please use the howmany parameter: "hello ". split ("", 3) // return ["h", "e", "l"]

Instance:

The above is all the content of this article. I hope you will like it.

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.