在 JavaScript 中,String substr()用于从字符串中提取子字符串,从指定位置开始并包含指定数量的字符。
用法:
string.substr(startIndex, length)
string:要从中提取子字符串的字符串。startIndex:开始提取字符的索引。如果为负数,则被视为(string.length + startIndex).length(可选):要提取的字符数。如果省略,则所有字符来自startIndex到字符串末尾的部分都包含在子字符串中。
例子:在这里,substr()方法被调用str字符串与startIndex的7和length的5。这会提取从索引处的字符开始的子字符串7(即“W”)并包含5此后的字符。结果子串是"World".
Javascript
const str = "Hello, World!";
const substring = str.substr(7, 5);
console.log(substring); // Output: "World"输出
World
相关用法
- JavaScript setInterval()用法及代码示例
- JavaScript string.anchor()用法及代码示例
- JavaScript string.replace()用法及代码示例
- JavaScript startsWith()用法及代码示例
- JavaScript setTimeout()用法及代码示例
- JavaScript split()用法及代码示例
- JavaScript Math cosh()用法及代码示例
- JavaScript Math sinh()用法及代码示例
- JavaScript Math sin()用法及代码示例
- JavaScript Math cos()用法及代码示例
- JavaScript Math tan()用法及代码示例
- JavaScript Math abs()用法及代码示例
- JavaScript Math pow()用法及代码示例
- JavaScript Math asin()用法及代码示例
- JavaScript Math acos()用法及代码示例
- JavaScript Math atan()用法及代码示例
- JavaScript Math ceil()用法及代码示例
- JavaScript Math floor()用法及代码示例
- JavaScript Math round()用法及代码示例
- JavaScript Math trunc()用法及代码示例
- JavaScript Math max()用法及代码示例
- JavaScript Math min()用法及代码示例
- JavaScript Math sqrt()用法及代码示例
- JavaScript Math sign()用法及代码示例
- JavaScript Math log()用法及代码示例
注:本文由纯净天空筛选整理自amanv09大神的英文原创作品 What is substr() Method in JavaScript ?。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
