// 使用 substr 方法
let str = "Hello, world!";
let result = str.substr(7, 5); // 从索引 7 开始,提取 5 个字符
console.log(result); // 输出: world
// 使用 substring 方法
let str2 = "Hello, world!";
let result2 = str2.substring(7, 12); // 从索引 7 开始,到索引 12 结束(不包括 12)
console.log(result2); // 输出: world
substr(startIndex, length):从指定的 startIndex 开始,提取指定长度的子字符串。如果只传入一个参数,则会从该位置开始提取到字符串末尾。substring(startIndex, endIndex):从 startIndex 开始,提取到 endIndex 之前(不包括 endIndex)的子字符串。如果只传入一个参数,则会从该位置开始提取到字符串末尾。上一篇:js 获取浏览器
下一篇:js substr()用法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站