Examples of split usages of string partitioning functions in JavaScript

Source: Internet
Author: User

Let's look at the following code:

1 2 3 4 5 6 <script type= "Text/javascript" > var str= "How are to doing today?" document.write (Str.split ("") + "<br/>") d Ocument.write (Str.split ("") + "<br/>") document.write (Str.split ("", 3)) </script>

The output results are 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,a,y,? How,are,you

Example:

1 2 "2:3:4:5". Split (":")//will return ["2", "3", "4", "5"] "|a|b|c". Split ("|") will return ["", "a", "B", "C"]

Using the following code, you can split a sentence into words:

1 var words = Sentence.split (')

If you want to split the word into letters, or split the string into characters, use the following code:

1 "Hello". Split ("")//May return ["H", "E", "L", "L", "O"]

To return only a subset of the characters, use the Howmany parameter:

1 "Hello". Split ("", 3)//May return ["H", "E", "L"]

or use regular expressions as separator:

1 var words = Sentence.split (/s+/)
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.