js字符串函数之split()join()

split方法用于把一个字符串切割成字符串数组,与join相反

一个参数表示以该参数为切割点,

1     var str="silence‘s world";
2     console.log(str.split(" "));// ["silence‘s", "world"]
3     console.log(str.split("‘"));// ["silence", "s world"]
4     console.log(str.split(""));//["s", "i", "l", "e", "n", "c", "e", "‘", "s", " ", "w", "o", "r", "l", "d"]
5   //两个参数时,后一个参数表示长度
6     console.log(str.split("‘",1));// ["silence"]
7     console.log(str.split("",5));// ["s", "i", "l", "e", "n"]

join方法用于将数组连接成字符串

1 var arr=["s","i","l","e","n","c","e"]
2     console.log(arr.join(","));//s,i,l,e,n,c,e
3     console.log(arr.join(""));//silence
4     console.log(arr.join("."));//s.i.l.e.n.c.e
5     console.log(arr.join("|"));//s|i|l|e|n|c|e

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。