Home  >  Article  >  Web Front-end  >  Usage and definition of js split js split splits a string into an array example code

Usage and definition of js split js split splits a string into an array example code

高洛峰
高洛峰Original
2017-01-18 15:47:141571browse

There is not much else to say about the usage of js split. Let me give you an example directly.

<script language="javascript"> 
str="2,2,3,5,6,6"; //这是一字符串 
var strs= new Array(); //定义一数组 
strs=str.split(","); //字符分割 
for (i=0;i<strs.length ;i++ ) 
{ 
document.write(strs[i]+"<br/>"); //分割后的字符输出 
} 
</script>

The output result is
2
2
3
5
6
6

js split is to split a string into multiple strings using specific characters. You should understand it at a glance.

The following is the definition and usage of js split, official reference.

Definition and usage
split() method is used to split a string into a string array.

Syntax
stringObject.split(separator,howmany)

Parameter 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).

For more usage and definitions of js split, please pay attention to the PHP Chinese website for related articles on the example code of js split splitting a string into an array!

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