当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JavaScript slice() 方法

JavaScript
JS 介绍
JS 怎样使用
JS 在哪使用
JS 变量
JS If...Else
JS Switch
JS 操作符
JS Popup Boxes
JS 函数
JS For 循环
JS While 循环
JS Break 循环
JS For...In
JS 事件
JS Try...Catch
JS Throw
JS onerror
JS 特殊字符
JS Guidelines
JS 对象介绍

JavaScript slice() 方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 372 ::
收藏到网摘: n/a

Definition and Usage
定义与用法

The slice() method extracts a part of a string and returns the extracted part in a new string.
slice()方法从一个字符串选取一部分字符,并将它们返回成一个新的字符串

Syntax
语法

stringObject.slice(start,end)

Parameter
参数
Description
注释
start
起始位置
Required. Specify where to start the selection. Must be a number
必选项。指定从哪里开始选取,必须为数字
end
终止位置
Optional. Specify where to end the selection. Must be a number
可选项。指定选取结束的位置,必须为数字


Tips and Notes
注意

Tip: You can use negative index numbers to select from the end of the string.
提示:你可以用一个负的起始值从字符串的末尾开始选取字符

Note: If end is not specified, slice() selects all characters from the specified start position and to the end of the string.
注意:如果没有指定结束的位置,slice()将选取从指定起始位置开始到字符串结束的所有字符


Example 1
实例1

In this example we will extract all characters from a string, starting at position 6:
在本例中,我们将选取字符串中第6个字符以后的所有字符:

<script type="text/javascript">
var str="Hello happy world!"
document.write(str.slice(6))
</script>

The output of the code above will be:
输出结果为:

happy world!


Example 2
实例2

In this example we will extract all the characters from position 6 to position 11:
在本例中,我们将从字符串中选取第6个字符到第11个字符间的字符:

<script type="text/javascript">
var str="Hello happy world!"
document.write(str.slice(6,11))
</script>

The output of the code above will be:
输出结果为:

happy


Try-It-Yourself Demos
互动演练

slice()
How to use slice() to extract characters from a string.
如何用slice()从字符串中选取字符

评论 (0) All

登陆 | 还没注册?