substr和slice都是用来从字符串中提取子字符串的方法,但它们有一些不同之处:
const str = "Hello, World!";
console.log(str.substr(7, 5)); // Output: World
const str = "Hello, World!";
console.log(str.slice(7, 12)); // Output: World
总的来说,substr更适合根据起始位置和长度来提取子字符串,而slice更适合根据起始位置和结束位置来提取子字符串。在实际使用中,根据具体需求选择合适的方法。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:C++ string.substr和slice区别