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

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


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

登陆 | 还没注册?