


Usage examples of string splitting function split in JavaScript_javascript skills
May 16, 2016 pm 04:05 PMThe 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?" document.write(str.split(" ") + "<br />") document.write(str.split("") + "<br />") document.write(str.split(" ",3)) </script>
The output results are 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(":") //将返回["2", "3", "4", "5"] "|a|b|c".split("|") //将返回["", "a", "b", "c"]
Use the following code to split sentences into words:
var words = sentence.split(' ')
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"]
If you only need to return a part of the characters, please use the howmany parameter:
"hello".split("", 3) //可返回 ["h", "e", "l"]
Or use regular expression as separator:
var words = sentence.split(/\s+/)
I hope this article will be helpful to everyone’s JavaScript programming design.

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of the method of converting int type to string in PHP

How to determine whether a Golang string ends with a specified character

How to check if a string starts with a specific character in Golang?

How to repeat a string in python_python repeating string tutorial

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP

PHP string manipulation: a practical way to effectively remove spaces

PHP String Matching Tips: Avoid Ambiguous Included Expressions

PHP techniques for deleting the last two characters of a string
