Home  >  Article  >  Web Front-end  >  How to use split() in JS (split a string into an array according to specified symbols)

How to use split() in JS (split a string into an array according to specified symbols)

高洛峰
高洛峰Original
2016-12-09 11:47:471708browse

No more nonsense, let me just post the code for you.

<!DOCTYPE html>
<html>
<head>
<meta charset="{CHARSET}">
<title></title>
</head>
<body>
</body>
<script type="text/javascript">
//在Javascript脚本中,将字符串按指定符号进行分割成数组用split()的方法
//格式StringObject.split( char [,howmany]);
//char: 必需。字符串或正则表达式howmany: 可选。
//该参数可指定返回的数组的最大长度。
//如果设置了该参数,返回的子串不会多于这个参数指定的数组。
//如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
var arr1="2:3:4:5".split(":");
document.write(arr1+"<br/>");
document.write(typeof(arr1)+"<br/>");
var arr2="|d|b|g".split("|");
document.write(arr2+"<br/>");
document.write(typeof(arr2)+"<br/>");
//希望把单词分割为字母,或者把字符串分割为字符,可使用下面的代码:
var arr3="love".split("");
document.write(arr3+"<br/>");
document.write(typeof(arr3)+"<br/>");
//以上操作实际上返回的是一个数组,具体访问某个元素,用arr[0],arr[1].......
</script>
</html>


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