Home >Web Front-end >JS Tutorial >javascript function split
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>