Home > Web Front-end > JS Tutorial > Usage examples of string splitting function split in JavaScript_javascript skills

Usage examples of string splitting function split in JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:05:20
Original
965 people have browsed it

The example in this article describes the usage of the string splitting function split in JavaScript. Share it with everyone for your reference. The details are as follows:

First look at the following code:

<script type="text/javascript">
var str="How are you doing today&#63;"
document.write(str.split(" ") + "<br />")
document.write(str.split("") + "<br />")
document.write(str.split(" ",3))
</script>
Copy after login

The output results are as follows:

How,are,you,doing,today&#63;
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,&#63;
How,are,you
Copy after login

Example:

"2:3:4:5".split(":") //将返回["2", "3", "4", "5"]
"|a|b|c".split("|") //将返回["", "a", "b", "c"]
Copy after login

Use the following code to split sentences into words:

var words = sentence.split(' ')
Copy after login

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

"hello".split("") //可返回 ["h", "e", "l", "l", "o"]
Copy after login

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

"hello".split("", 3)  //可返回 ["h", "e", "l"]
Copy after login

Or use regular expression as separator:

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

I hope this article will be helpful to everyone’s JavaScript programming design.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template