JavaScript中join()、splice()、slice()和split()函数用法示例

本文实例讲述了JavaScript中join()、splice()、slice()和split()函数用法。分享给大家供大家参考,具体如下:

join()

join() 方法用于把数组中的所有元素放入一个字符串。

元素是通过指定的分隔符进行分隔的。

<script type="text/javascript">

var arr = new Array(3)

arr[0] = "George"

arr[1] = "John"

arr[2] = "Thomas"

document.write(

arr.join()

)

</script>

运行结果:

George,John,Thomas

<script type="text/javascript">

var arr = new Array(3)

arr[0] = "George"

arr[1] = "John"

arr[2] = "Thomas"

document.write(

arr.join(".")

)

</script>

运行结果:

George.John.Thomas

split()

split() 方法用于把一个字符串分割成字符串数组。

<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

"2:3:4:5".split(":") //将返回["2", "3", "4", "5"]

"|a|b|c".split("|") //将返回["", "a", "b", "c"]

"hello".split("") //可返回 ["h", "e", "l", "l", "o"]

"hello".split("", 3) //可返回 ["h", "e", "l"]

slice()

slice() 方法可从已有的数组中返回选定的元素。

<script type="text/javascript">

var arr = new Array(3)

arr[0] = "George"

arr[1] = "John"

arr[2] = "Thomas"

document.write(arr + "<br />")

document.write(

arr.slice(1)

+ "<br />")

document.write(arr)

</script>

运行结果:

George,John,Thomas

John,Thomas

George,John,Thomas

<script type="text/javascript">

var arr = new Array(6)

arr[0] = "George"

arr[1] = "John"

arr[2] = "Thomas"

arr[3] = "James"

arr[4] = "Adrew"

arr[5] = "Martin"

document.write(arr + "<br />")

document.write(

arr.slice(2,4)

+ "<br />")

document.write(arr)

</script>

运行结果:

George,John,Thomas,James,Adrew,Martin

Thomas,James

George,John,Thomas,James,Adrew,Martin

splice()

splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。

注释:该方法会改变原始数组。

<script type="text/javascript">

var arr = new Array(6)

arr[0] = "George"

arr[1] = "John"

arr[2] = "Thomas"

arr[3] = "James"

arr[4] = "Adrew"

arr[5] = "Martin"

document.write(arr + "<br />")

arr.splice(2,0,"William")

document.write(arr + "<br />")

</script>

运行结果:

George,John,Thomas,James,Adrew,Martin

George,John,William,Thomas,James,Adrew,Martin

<script type="text/javascript">

var arr = new Array(6)

arr[0] = "George"

arr[1] = "John"

arr[2] = "Thomas"

arr[3] = "James"

arr[4] = "Adrew"

arr[5] = "Martin"

document.write(arr + "<br />")

arr.splice(2,1,"William")

document.write(arr)

</script>

运行结果:

George,John,Thomas,James,Adrew,Martin

George,John,William,James,Adrew,Martin

<script type="text/javascript">

var arr = new Array(6)

arr[0] = "George"

arr[1] = "John"

arr[2] = "Thomas"

arr[3] = "James"

arr[4] = "Adrew"

arr[5] = "Martin"

document.write(arr + "<br />")

arr.splice(2,3,"William")

document.write(arr)

</script>

运行结果:

George,John,Thomas,James,Adrew,Martin

George,John,William,Martin

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试运行一下文中所述代码。

更多关于JavaScript相关内容可查看本站专题:《JavaScript常用函数技巧汇总》、《JavaScript字符与字符串操作技巧总结》、《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript数组操作技巧总结》

希望本文所述对大家JavaScript程序设计有所帮助。

以上是 JavaScript中join()、splice()、slice()和split()函数用法示例 的全部内容, 来源链接: utcz.com/z/349661.html

回到顶部