Home  >  Article  >  Web Front-end  >  How to use the javascript substring() method

How to use the javascript substring() method

青灯夜游
青灯夜游Original
2022-03-08 18:14:526109browse

In JavaScript, the substring() method is used to extract the characters between two specified subscripts in the string. The syntax is "string.substring(from,to)"; the substring returned by this method includes The characters at the "from" parameter, but not including the characters at the "to" parameter.

How to use the javascript substring() method

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, the substring() method is used to extract characters between two specified subscripts in a string.

Syntax:

string.substring(from, to)
Parameters Description
from Required. A nonnegative integer that specifies the position of the first character of the substring to be extracted in the string Object.
to Optional. A non-negative integer that is one more position in the string Object than the last character of the substring to be extracted.
If this parameter is omitted, the returned substring will go to the end of the string. The substring returned by the

substring() method includes the characters starting at from, but does not include the characters ending at to .

Example:

var str="Hello world!";
console.log(str.substring(3,7));

How to use the javascript substring() method

If the second parameter is omitted, it means to extract all characters from the starting position to the end.

var str="Hello world!";
console.log(str.substring(3));

How to use the javascript substring() method

[Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of How to use the javascript substring() method. For more information, please follow other related articles on the PHP Chinese website!

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