- split:分割
- splice:粘接
String.split()
split() 方法用于把一个字符串分割成字符串数组。
语法:
stringObject.split(separator,howmany)//分离器必选,返回数组最大长度可选
例子:
<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>
结果:
How,are,you,doing,today?
H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
How,are,you
Array.splice()
splice() 方法用于插入、删除或替换数组的元素。
语法:
array.splice(index,howmany,item1,.....,itemX)
//index从何处开始必须;删除个数必须不填删除从index到结尾;item1, ..., itemX可选,为添加到数组的新元素
添加实例:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,1,"Lemon","Kiwi");
运行结果:
Banana,Orange,Lemon,Kiwi,Mango
删除实例:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,2);
运行结果:
Banana,Orange