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

 

你可能感兴趣的:(split)