1. split():将字符串按照指定的分隔符进行分割,返回一个数组。

例:将字符串 "hello world" 按照空格分割成数组。

let str = "hello world";
let arr = str.split(" ");
console.log(arr); // ["hello", "world"]
  1. slice():从字符串中截取指定位置的子串,返回一个新的字符串。

例:将字符串 "hello world" 截取出 "world"。

let str = "hello world";
let newStr = str.slice(6);
console.log(newStr); // "world"
  1. substring():从字符串中截取指定位置的子串,返回一个新的字符串。

例:将字符串 "hello world" 截取出 "world"。

let str = "hello world";
let newStr = str.substring(6);
console.log(newStr); // "world"
  1. substr():从字符串中截取指定长度的子串,返回一个新的字符串。

例:将字符串 "hello world" 截取出 "world"。

let str = "hello world";
let newStr = str.substr(6, 5);
console.log(newStr); // "world"
``
js 分割截取

原文地址: http://www.cveoy.top/t/topic/g1if 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录