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.
substr()
method is used for extraction The specified part of the string.
<code>substr(start [,length])</code>
Extracted substring.
Extract the part of the string starting from the specified position:
<code>const str = "Hello World"; console.log(str.substr(6)); // 输出 "World"</code>
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>
length
parameter is not provided, extract from the start
position to the end of the string of all characters. start
is negative, counting starts from the end of the string. start
exceeds the string length, no characters will be extracted. 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!