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

JavaScript
JS数学对象参考
JS字符串对象参考
JS函数参考
JS事件参考
Javascript 常用正则表达式
FF和IE下的js兼容性问题
jQuery 简单介绍
jQuery / 核心 / $(expression, [context] ) 函数
jQuery / 核心 / $(html) 函数
如何使用JS来判断浏览器类型(ie、firefox,等等)
Javascript在IE和FireFox中的不同表现
3个js字符编码函数区别
javascript 中的 XMLDOM 对象

JavaScript slice() 方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 374 ::
收藏到网摘: 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

登陆 | 还没注册?