- split()
方法 | 描述 | 返回信息 |
---|
split() | 字符串分割成字符串数组 | 返回字符串数组 |
let a = 'a|b|c|d';
console.log(a.split("|"));
let b = 'hello li lei';
console.log(b.split(' '));
console.log(b.split(''));
- substring()与substr()
方法 | 描述 | 返回信息 |
---|
substr(index)、substring(index) | 当只有一个参数index时,substr与substring一样,获取index到结尾的字符 | 返回一个字符串 |
substr(index,length) | 获取从index开始的length个字符 | 返回从一个字符串 |
substring(index,index2) | 获取从index开始到index2之前的字符 | 返回一个字符串 |
let a = 'abcd';
console.log(a.substr());
console.log(a.substring());
console.log(a.substr(1,2));
console.log(a.substring(1,3));
- concat()
方法 | 描述 | 返回信息 |
---|
concat() | 字符串拼接 | 返回连接后的字符串,不改变原字符串 |
let a = 'abc';
let b = 'def';
console.log(a.concat(b));
console.log(a);
- charAt()
方法 | 描述 | 返回信息 |
---|
charAt() | 默认下标为0,不在0—str.length之内返回空数组 | 返回指定索引位置的字符 |
let a = 'abcd';
console.log(a.charAt());
console.log(a.charAt(6));
- indexOf()
方法 | 描述 | 返回信息 |
---|
indexOf() | 查询字符串中值,查到返回索引 | 返回索引 |
let a = 'abcde';
console.log(a.indefOf('b'));
console.log(a.indexOf('e'));