// 示例代码:使用 JavaScript 切割字符串
// 定义一个字符串
let str = "Hello, world!";
// 使用 substring 方法切割字符串,从索引 0 开始到索引 5 结束(不包括索引 5)
let result1 = str.substring(0, 5);
console.log(result1); // 输出: Hello
// 使用 slice 方法切割字符串,从索引 7 开始到索引 12 结束(不包括索引 12)
let result2 = str.slice(7, 12);
console.log(result2); // 输出: world
// 使用 split 方法按指定分隔符切割字符串,返回一个数组
let result3 = str.split(", ");
console.log(result3); // 输出: ["Hello", "world!"]
// 使用 match 方法按正则表达式切割字符串,返回一个数组
let result4 = str.match(/[\w]+/g);
console.log(result4); // 输出: ["Hello", "world"]
start
索引开始,到 end
索引结束(不包括 end
),提取子字符串。substring
,但可以接受负数索引,表示从字符串末尾开始计算。上一篇:js 接口
下一篇:js 字符串切割
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站