Home > Web Front-end > JS Tutorial > body text

Usage of substr in js

下次还敢
Release: 2024-05-06 10:24:14
Original
965 people have browsed it

The substr() method in JS can extract the specified part of the string. The syntax is substr(start [,length]), where start specifies the extraction starting position and length specifies the extraction length. It can extract part of the string, starting from the start position to the end of the string or a specified length, without modifying the original string.

Usage of substr in js

Usage of substr() in JS

Function

substr() method is used for extraction The specified part of the string.

Syntax

<code>substr(start [,length])</code>
Copy after login

Parameters

  • start: Start extracting characters from this index position (counting from 0)
  • length: Optional, specifies the number of characters to extract

Return value

Extracted substring.

Usage example

Extract the part of the string starting from the specified position:

<code>const str = "Hello World";
console.log(str.substr(6)); // 输出 "World"</code>
Copy after login

Extract the part of the string starting from the specified position with the specified length Part:

<code>const str = "Hello World";
console.log(str.substr(6, 5)); // 输出 "World"</code>
Copy after login

Note:

  • If the length parameter is not provided, extract from the start position to the end of the string of all characters.
  • If start is negative, counting starts from the end of the string.
  • If start exceeds the string length, no characters will be extracted.
  • If length is negative, no characters are extracted.
  • substr() Does not modify the original string.

The above is the detailed content of Usage of substr in js. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!