Example of how to use the string split function in JavaScript

Source: Internet
Author: User
Tags split words

Example of how to use the string split function in JavaScript

Let's take a look at the following code:

1

2

3

4

5

6

<Script type = "text/javascript">

Var str = "How are you doing today? "

Document. write (str. split ("") + "<br/> ")

Document. write (str. split ("") + "<br/> ")

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

</Script>

The output result is as follows:

1

2

3

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:

1

2

"2: 3: 4: 5". split (":") // Returns ["2", "3", "4", "5"]

"| A | B | c". split ("|") // ["", "a", "B", "c"]

Use the following code to split sentences into words:

1

Var words = sentence. split ('')

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

1

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

1

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

Or use the regular expression as the separator:

1

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

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.