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

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


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

登陆 | 还没注册?