// 示例代码:使用 substring() 方法提取字符串的子串
let str = "Hello, world!";
let result1 = str.substring(0, 5); // 从索引 0 开始,到索引 5(不包括索引 5)
console.log(result1); // 输出: Hello
let result2 = str.substring(7, 12); // 从索引 7 开始,到索引 12(不包括索引 12)
console.log(result2); // 输出: world
// 如果只提供一个参数,则提取从该索引开始到字符串末尾的部分
let result3 = str.substring(7);
console.log(result3); // 输出: world!
// 注意:substring() 不区分正负索引,负索引会被视为 0
let result4 = str.substring(-5, 5);
console.log(result4); // 输出: Hello
substring(startIndex, endIndex)
方法用于提取字符串中介于两个指定下标之间的部分。startIndex
是提取的起始位置(包含),endIndex
是提取的结束位置(不包含)。endIndex
省略,则提取从 startIndex
到字符串末尾的部分。上一篇:js new url
下一篇:js 生成时间戳
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站