Home > Web Front-end > JS Tutorial > body text

How to use the array length obtained by the split() method in js

不言
Release: 2018-07-26 11:34:23
Original
2208 people have browsed it

The split() method is used to split a string into an array of strings. The content of this article is about how to use the split() method in js to get the array length. The content is very detailed. Friends in need can refer to it. I hope it can help you.

Definition and usage

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

Syntax

stringObject.split(separator,howmany)
Copy after login
ParametersDescription
separatorRequired. A string or regular expression to split the stringObject from where specified by this parameter.
howmanyOptional. 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

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

However, if separator is a regular expression that contains subexpressions, the returned array includes strings that match those subexpressions (but not text that matches the entire regular expression. ).

Let’s take a look at how to get the array length by split() method in js.

The split(",") method in js splits the string through ",". If there is no "," in the string, the string itself is returned

var str = “abc”;//分隔符个数为0
var newStr = str.split(",");
console.log(newStr.length);
Copy after login

The result is: Length: 1 newStr: abc

var str = “abc,”;//分隔符个数为1
var newStr = str.split(",");
console.log(newStr.length);
Copy after login

The result is: Length: 2 newStr: abc

That is to say, the length of split is related to the number of delimiters. The length of the split array is the number of delimiters plus one.

The split() method in java does not have this problem

Related recommendations:

How does vue.js implement tree table encapsulation? Methods of implementing tree tables in vue.js

Three methods to determine whether the elements on the js page are within the screen display area

The above is the detailed content of How to use the array length obtained by the split() method in js. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!