欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-13 12:42:10
本文实例讲述了javascript中join()、splice()、slice()和split()函数用法。分享给大家供大家参考,具体如下: join() join()...

本文实例讲述了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程序设计有所帮助。