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

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 substr() 方法


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

Definition and Usage
定义与用法

The substr() method extracts a specified number of characters in a string, from a start index.
substr()方法用于从字符串中从指定的起始位置选取指定长度的一段字符

Syntax
语法

stringObject.substr(start,length)

Parameter
参数
Description
注释
start
起始位置
Required.Where to start the extraction. Must be a numeric value
必选项。选取的起始位置。必须为数字。
length
长度
Optional. How many characters to extract. Must be a numeric value.
可选项。要选取字符的长度。必须为数字。


Tips and Notes
注意

Note: To extract characters from the end of the string, use a negative start number.
注意:要从字符的末尾开始选取字符,用一个负的起始值。

Note: The start index starts at 0.
注意:默认的起始位置为0

Note: If the length parameter is omitted, this method extracts to the end of the string.
注意:如果没有指定选取的长度,默认选取自起始位置起剩下的所有字符


Example
实例

In this example we will use substr() to extract some characters from a string:
在本例中,我们将用substr()从一个字符串中选取一些字符:

<script type="text/javascript">
var str="Hello world!"
document.write(str.substr(3))
</script>

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

lo world!


Example
实例

In this example we will use substr() to extract some characters from a string:
在本例中,我们将用substr()从一个字符串中选取一些字符:

<script type="text/javascript">
var str="Hello world!"
document.write(str.substr(3,7))
</script>

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

lo worl


Try-It-Yourself Demos
互动演练

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

评论 (0) All

登陆 | 还没注册?