首页 技术 正文
技术 2022年11月15日
0 收藏 926 点赞 2,642 浏览 858 个字
slice() 方法可从已有的数组中返回选定的元素。string.slice( beginslice [, endSlice] );

下面是参数的详细信息:

  • beginSlice : 从零开始的索引位置开始提取
  • endSlice : 从零开始的索引位置结束提取。如果省略,切片中提取的字符串的末尾

注意:作为一个负指数,endSlice表示从字符串末尾的偏移。 string.slice(2,-1)提取第二到最后一个字符,字符串的第三个字符。
返回值:

如果成功的话,切片返回字符串内的正则表达式的索引。否则,返回-1。

实例

例子 1
在本例中,我们将创建一个新数组,然后显示从其中选取的元素:
<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例子 2
在本例中,我们将创建一个新数组,然后显示从其中选取的元素:
<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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,367
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,859