目录
1、 查询字符串长度(length)
2、charAt
3、 连接字符串(concat)
4、提取字符串的三种方法(slice,substr,substring)
4-1、 slice
4-2、 substr
5、分割字符串(split)
6、字符串索引
6-1、indexOf
6-2、lastIndexOf
7、字符串检查方法(startWith、endWith、includes)
8、两侧空白处理(trim、trimLeft、trimRight)
9、字符串重复拼接方法(repeat)
10、 迭代方法
11、 字符串解构成数组
12、首字母大写
13、字符串替换方法 (replace)
14、字符串比较方法 (localCompare)
1、 查询字符串长度(length)
let str = 'Hello world!';
// 查询字符串长度 双字节字符也会按单字节来计算
let leng = str.length;
console.log(leng); //12
2、charAt
//charAt 返回指定索引位置上的字符
let char = str.charAt(3);
console.log(char); // l
3、 连接字符串(concat)
// 用于将一个或多个字符串拼接成一个新的字符串 不改变原来的字符串变量 concat参数可以是任意多个
let newstr1 = str.concat('Hello','everyone');
console.log(newstr1); // Hello world!Helloeveryone
console.log(str); // Hello world!
4、提取字符串的三种方法(slice,substr,substring)
三种提取字符串的方法都不会改变原来的字符串
4-1、 slice
// 1、slice 第一个参数表示截取的起始位置
// 第二个参数表示提取的结束位置, 提取范围从起始位置到结束位置之前
// 省略第二个参数意味着一直取到字符串末尾
// slice 方法的参数如果有负值,则自动将负值转换成字符串的长度加上该负值
let slice1 = str.slice(1,5);
console.log(slice1); // ello
let slice2 = str.slice(1);
console.log(slice2); // ello world!
let slice3 = str.slice(1,-3);
console.log(slice3); // ello wor
4-2、 substr
// 2、substr 第一个参数表示截取的起始位置
// 第二个参数表返回的子字符串的长度
// substr 方法中如果参数有负值 则将第一个参数装换成字符串的长度加上该值,将第二个负参数转换为 0
let substr1 = str.substr(1,5);
console.log(substr1); // ello
let substr2 = str.substr(1);
console.log(substr2); // ello world!
let substr3 = str.substr(1,-3);
console.log(substr3); // (空)
4-3、 substring
// 3、substring 第一个参数表示截取的起始位置
// 第二个参数表示提取的结束位置, 提取范围从起始位置到结束位置之前
// substring 方法中所有的负参数都自动转换为 0
let substring1 = str.substring(1,5);
console.log(substring1); // ello
let substring2 = str.substring(1);
console.log(substring2); // ello world!
let substring3 = str.substring(1,-3); // 先转换为substring(1,0) 在转换为 substring(0,1)
console.log(substring3); // H
5、分割字符串(split)
const str = ‘apple、abande’;
const arr = str.spli(‘、’);
console.log(arr); // [‘apple’,’abande’]
6、字符串索引
6-1、indexOf
// 返回索引位置,如果没有,则返回-1
let index1 = str.indexOf('wor');
console.log(index1); // 6
6-2、lastIndexOf
// 返回索引位置,如果没有,则返回-1
let index2 = str.lastIndexOf('one');
console.log(index2); //-1
7、字符串检查方法(startWith、endWith、includes)
//字符串包含方法 返回 boolean值
//当只接受第一个参数时,includes、startWith从索引 0 开始检查;
//endWith从索引(原字符串.length-要查找的字符串.length)
//当接受第二个参数时,includes、startWith从 n开始检查,endWith的第二个参数是末尾的位置
let bool01 = str.startsWith('hel');
console.log(bool01);
let bool02 = str.startsWith('llo',2);
console.log(bool02);
let bool03 = str.endsWith('orld');
console.log(bool03);
let bool04 = str.endsWith('orl',9);
console.log(bool04);
let bool05 = str.includes('o w');
console.log(bool05);
let bool06 = str.includes('o w',4);
console.log(bool06);
8、两侧空白处理(trim、trimLeft、trimRight)
9、字符串重复拼接方法(repeat)
//接受一个整数参数n,将字符串复制 n次 然后返回拼接所有副本后的结果
let newstr2 = str.repeat(3);
console.log(newstr2); // Hello world!Hello world!Hello world!
10、 迭代方法
let message = 'will';
let string = message[Symbol.iterator]();
console.log(string.next()); //{ value: 'w', done: false }
console.log(string.next()); //{ value: 'i', done: false }
console.log(string.next()); //{ value: 'l', done: false }
console.log(string.next()); //{ value: 'l', done: false }
console.log(string.next()); //{ value: undefined, done: true }
for(const x of 'abcdef'){
console.log(x); //a b c d e f
}
11、 字符串解构成数组
let message1 = 'abcdefg';
let arr = [...message1];
console.log(arr); //['a', 'b', 'c','d', 'e', 'f','g']
12、首字母大写
toLowerCase
toLocalLowerCase
toUpperCase
toLocalUpperCase
13、字符串替换方法 (replace)
// 该方法接受两个参数,第一个可以是字符串或正则表达式
// 第二个参数可以是字符串或一个函数
// 注意 如果第一个参数是字符串,那么只会替换第一个子字符串,
// 如果要替换掉全部匹配的子字符串,需要使用正则表达式并带上全局标记 g
let text = 'cat, bat, sat, fat';
let result = text.replace('at','ond');
console.log(result); //cond, bat, sat, fat
let result1 = text.replace(/at/g,'ond');
console.log(result1); //cond, bond, sond, fond
14、字符串比较方法 (localCompare)
// 按照字母表顺序,若原字符串应排在用于比较的字符串的前头,则返回负值
// 在这里 原字符串为 'blue',用于比较的字符串为'yellow'
let msg4 = 'blue';
let msg5 = 'yellow';
console.log(msg4.localeCompare(msg5)); // -1