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

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


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

Definition and Usage
定义与用法

The substring() method extracts the characters in a string between two specified indices.
substring()方法用于取出字符串中特定位置间的字符

Syntax
语法

stringObject.substring(start,stop)

Parameter
参数
Description
注释
start
开始位置
Required. Where to start the extraction. Must be a numeric value
必选项。开始选取的位置。必须为数字。
stop
停止
Optional. Where to stop the extraction. 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 stop parameter is omitted, this method extracts to the end of the string.
注意:如果为指定结束位置,默认将选取到字符串结束。


Example
实例

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

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

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

lo world!


Example
实例

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

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

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

lo w


Try-It-Yourself Demos
互动演练

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

评论 (0) All

登陆 | 还没注册?