1. charAt() [不常用]
=> 语法: 字符串.charAt(索引)
=> 返回值: 获取该索引位置的字符
他分为有和没有 假如一个字符串长度是5 你获取索引位置为3肯定有 你获取索引位置100肯定没有
-> 如果有该索引位置, 就是索引位置字符
-> 如果没有该索引位置, 是一个空 照样输出 但是什么也没有
1. charsAt()方法使用
var str = 'hello world';
var res = str.charAt(1);
console.log(res); //e
var res2 = str.charAt(100);
console.log(res2); //空
2. charCodeAt() [不常用]
=> 语法: 字符串.charCodeAt(索引)
=> 返回值: 获取该索引位置的字符的字符编码(UTF-8编码)
因为我们在网页上用的是utf8字符集 我们应该拿的是二进制 但是是以10进制形式输出的 所以最后是十进制表示的字符编码
2. charCodeAt()方法使用
var str = 'hello world';
var res = str.charCodeAt(4);
console.log(res);
总结:前两种方式不常用 作为了解 因为第二种你想获取某个字符的字符编码基本用不到
第一种你想获取字符串里面某一个字符 我们可以通过str[索引] 形式也可以
3. substr()
=> 语法: 字符串.substr(开始索引, 截取多少个)
=> 作用: 截取字符串
=> 返回值: 截取出来的字符串
3. substr()方法使用
var str = 'hello world';
// 表示从索引[2]开始 向后数五个字符位置
var res = str.substr(2, 5);
console.log(res); //llo w
4. substring()
=> 语法: 字符串.substring(开始索引, 结束索引) - 包前不包后
=> 作用: 截取字符串
=> 返回值: 截取出来的字符串
4. substring()方法使用
var str = 'hello world';
// 从索引[2] 开始 截取到索引[5]位置 但是不包含索引[5]
var res = str.substring(2, 5);
console.log(res); // llo
5. toLowerCase()
=> 语法: 字符串.toLowerCase()
=> 作用: 把字符串里面的大写字母转成小写字母
=> 返回值: 转换好以后的字符串
5. toLowerCase() 方法使用
var str = 'hello WORLD'; //将大写字母转化为小写字母 它只能够转字母 转不了的就原样输出
var res = str.toLowerCase();
console.log(res);
6. toUpperCase()
=> 语法: 字符串.toUpperCase()
=> 作用: 把字符串里面的小写字母转换成大写字母
=> 返回值: 转换好以后的字符串
6.toUpperCase()方法使用
var str = 'helLO wolrd'; //将小写字母转换为大写字母 转换了的就转换 转换不了的就原样输出
var res = str.toUpperCase();
console.log(res);
7. replace()
=> 语法: 字符串.replace('要被替换的字符', '替换成的字符')
=> 作用: 替换字符串内的某些字符
-> 只能替换查找到的第一个 如果找不到要被替换的字符 他就什么也不替换 原样输出就可以了
=> 返回值: 替换好的字符串
7. replace()方法使用 只能够替换查找到得第一个内容
var str = '你好 世界 H H abc HH 你好';
console.log(str);
var res = str.replace('a', '***');
console.log(res);
8. concat() 这个方法和数组同名 字符串也有 这个方法也不常用 因为我用他不如用加号 加号拼接更加好用
=> 语法: 字符串.concat(字符串)
=> 作用: 拼接字符串
=> 返回值: 拼接好的字符串
8. concat()方法使用 这个方法基本不使用 因为我们完全可以使用加号拼接更加的方便
var str = 'hello';
// var str = 'hello' + 'world';
// console.log(str);
var res = str.concat('world');
console.log(res);
9. slice()
=> 语法: 字符串.slice(开始索引, 结束索引) - 包前不包后
-> 和 substring 的区别就是可以写 负整数
-> 当你写负整数的时候, 表示 length + 负整数
=> 作用: 截取字符串
=> 返回值: 截取好的字符串
9. slice()方法使用
var str = 'hello world';
var res = str.slice(2, 8);
console.log(res); // llo wo 但是r取不到 因为包前不包后 他和substring的区别是参数可以写成负整数
var res2 = str.slice(2, -3);
console.log(res2); //这里的 -3 其实就是str的长度 11-3 还是8 跟上面的res输出是一样的
10. split()
=> 语法: 字符串.split('切割符号', 多少个)
-> 切割符号, 按照你写的符号把字符串切割开 就是分割开
+ 如果不写, 那么就直接切割一个完整的
+ 如果写一个空字符串(''), 按照一位一位的切割
-> 多少个, 选填, 默认是全部, 表示你切割完以后保留多少个
=> 返回值: 一个数组的形式保存每一段内容
-> 不管按照什么切割, 返回值一定是一个数组
10. split()方法使用
var str = '2021-12-12';
var res = str.split(); //什么也不写 那就直接切割成一个完整的
var res = str.split(''); //按照一位一位的切割
var res = str.split('-'); //用中划线将这个字符串切割成3段
var res = str.split('-', 1); //用中划线将这个字符串切割分段 如果我这里写一个1 就表示只保留第一段
var res = str.split('-', 2); //用中划线将这个字符串切割分段 如果我这里写一个1 就表示只保留前两段
console.log(res);