Home  >  Article  >  Web Front-end  >  Getting started with the substring method of JavaScript string objects (used to intercept strings)_Basic knowledge

Getting started with the substring method of JavaScript string objects (used to intercept strings)_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:33:371465browse

JavaScript substring method

The

substring method is used to intercept a string by specifying the start and end positions and return the intercepted part of the string. Its syntax is as follows:

Copy code The code is as follows:

str_object.substring(start, end)

参数 说明
str_object 要操作的字符串(对象)
start 必需。开始截取的位置,非负整数
end 可选。字符串截取结束的位置,非负整数;如果省略,则至字符串结束为止

Tip: If the parameters start and end are equal, then this method returns an empty string. If start is greater than end, the method will swap the two parameters before intercepting the string.

substring method instance

Copy code The code is as follows:



Running this example, output:
Copy code The code is as follows:

bc

bc

The difference between substring, slice and substr

1.substring cannot accept negative parameters, but the slice method can
2. The second parameter of substring is to specify the end position of interception, and the second parameter of substr method is to specify the length of string interception

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