Home  >  Article  >  Web Front-end  >  javascript function split

javascript function split

伊谢尔伦
伊谢尔伦Original
2016-11-22 13:29:371051browse

Function: 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 containing subexpressions, then the returned array includes strings matching those subexpressions (but not text matching the entire regular expression).

Instance 1:

<html>
<head>
<title>使用指定的字符分割字符串</title>
</head>
<body>
<script language="javascript">
<!--
 name = "张三,李四,王五";
 ch = new Array;
 ch = name.split(",");
 for(i=0;i<ch.length;i++){
  document.write(ch[i],"<br>");
 }
//-->
</script>
</body>
</html>

Instance 2:

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


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