Home  >  Article  >  Web Front-end  >  How to use the array length obtained by the split() method in js

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

不言
不言Original
2018-07-26 11:34:232219browse

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)
Parameters Description
separator Required. A string or regular expression to split the stringObject from where specified by this parameter.
howmany 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

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);

The result is: Length: 1 newStr: abc

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

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!

Statement:
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