Home > Web Front-end > Front-end Q&A > How to use split method in javascript

How to use split method in javascript

WBOY
Release: 2022-07-28 17:04:08
Original
9073 people have browsed it

The split method is used in JavaScript to split a string into a string array. The syntax is "string.split (string or regular expression, returns the maximum length of the array)"; if not specified, return length of the array, the entire string will be split, and the return result of this method is a string array.

How to use split method in javascript

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

How to use the split method in javascript

The split() method is used to split a string into a string array.

Tip: If you use the empty string ("") as a separator, each character in the stringObject will be split.

Note: The split() method does not change the original string.

Syntax

string.split(separator,limit)
Copy after login

Parameter Description

separator Optional. A string or regular expression to split the string Object from where specified by this parameter.

limit Optional. This parameter specifies the maximum length of the returned array. If this parameter is set, no more substrings will be returned than the array specified by this parameter. If this parameter is not set, the entire string will be split regardless of its length.

Return value

Array A string array. The array is created by splitting the string string Object into substrings at the boundaries specified by separator. The strings in the returned array do not include the separator itself.

The example is as follows:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
</head>
<body>
<p id="demo">单击按钮显示分割后的数组</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction(){
var str="How are you doing today?";
var n=str.split(" ",3);
document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>
Copy after login

Output result:

How to use split method in javascript

[Related recommendations: javascript video tutorialweb front-end

The above is the detailed content of How to use split method in javascript. For more information, please follow other related articles on the PHP Chinese website!

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