Example of how to use the string split function in JavaScript: javascriptsplit

Source: Internet
Author: User
Tags split words

Example of how to use the string split function in JavaScript: javascriptsplit

This document describes the use of the string splitting function in JavaScript. Share it with you for your reference. The details are as follows:

Let's take a look at the following code:

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

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: 3: 4: 5 ". split (":") // Returns ["2", "3", "4", "5"] "| a | B | c ". split ("|") // Returns ["", "a", "B", "c"]

Use the following code to split sentences into words:

var words = sentence.split(' ')

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

Or use the regular expression as the separator:

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

I hope this article will help you design javascript programs.

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.