Home  >  Article  >  Web Front-end  >  Detailed explanation of JavaScript substr() string interception function_Basic knowledge

Detailed explanation of JavaScript substr() string interception function_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 17:36:351356browse

substr Definition and Usage The
substr() method can extract the specified number of characters starting from the start subscript in a string.

Syntax
stringObject.substr(start,length)

参数 描述
start 必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。
length 可选。子串中的字符数。必须是数值。如果省略了该参数,那么返回从 stringObject 的开始位置到结尾的字串。

Description
If length is 0 or negative, an empty string will be returned. If this parameter is not specified, the substring will be continued to the end of stringvar.

Example
The following example demonstrates the use of the substr method.

The first character to intercept the string.


[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

The result is: j

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]


It means interception Starting from the 12th character in the string, the length is 5

In this example we will use substr() to extract some characters from a string:

[Ctrl A Select All Note: If you need to introduce external Js, you need to refresh to execute

]

Output:

lo world!

Example 2


[Ctrl A Select All Note:

If you need to introduce external Js, you need to refresh to execute

]

Output:

lo worl
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn