js 分割截取
- split():将字符串按照指定的分隔符进行分割,返回一个数组。
例:将字符串 "hello world" 按照空格分割成数组。
let str = "hello world";
let arr = str.split(" ");
console.log(arr); // ["hello", "world"]
- slice():从字符串中截取指定位置的子串,返回一个新的字符串。
例:将字符串 "hello world" 截取出 "world"。
let str = "hello world";
let newStr = str.slice(6);
console.log(newStr); // "world"
- substring():从字符串中截取指定位置的子串,返回一个新的字符串。
例:将字符串 "hello world" 截取出 "world"。
let str = "hello world";
let newStr = str.substring(6);
console.log(newStr); // "world"
- substr():从字符串中截取指定长度的子串,返回一个新的字符串。
例:将字符串 "hello world" 截取出 "world"。
let str = "hello world";
let newStr = str.substr(6, 5);
console.log(newStr); // "world"
``
原文地址: http://www.cveoy.top/t/topic/g1if 著作权归作者所有。请勿转载和采集!