一、常用方法
说明:**所有字符串的方法都不会改变原始字符串
1. charAt()
=> 语法: 字符串.charAt(索引)
=> 返回值: 该索引位置的字符串
-> 如果有该索引的位置,就是索引字符串
-> 如果没有索引位置,是一个空
var str = 'hello world'
var res = str.charAt(6)
console.log(res);
var res2 = str.charAt(66)
console.log(res2);
2. charCodeAt()
=> 语法: 字符串.charCodeAt(索引)
=> 返回值: 该索引位置的字符编码(UTF8)
var str = 'hello world'
var res = str.charCodeAt(1) //取到下标的 ASCII 编码
console.log(res);
3. substr()
=> 语法: 字符串.substr(开始索引,多少个) => 作用: 截取字符串 => 返回值: 截取出来的字符串
var str = 'hello world'
//从索引 [2] 开始,向后数5个字符位置
var res = str.substr(2,5)
console.log(res);
4. substring()
=> 语法: 字符串.substring(开始索引,结束索引) - 包前不包后 => 作用 :截取字符串 => 返回值 : 截取出来的字符串
var str = 'hello world'
//从索引 [2]开始截取到[5] 不包括[5]
var res = str.substring(2,5)
console.log(res);
5. toLowerCase()
=> 语法: 字符串.toLowerCase() => 作用:把字符串里面的大写字母转成小写 => 返回值: 转换好以后的字符串
var str = 'HELLO worLD' //只能转大写字母 转不了的原样输出
var res = str.toLowerCase()
console.log(res);
6. toUpperCase()
=> 语法: 字符串.toUpperCase() => 作用: 把字符串里面的小写字母转换成大写 => 返回值: 转换好以后的字符串
var str = 'HELLo WorlD'
var res = str.toUpperCase()
console.log(res);
7. replace()
=> 语法: 字符串.replace('要被替换的字符','替换成的字符')
=> 作用: 替换字符串内的某些字符
->只能替换查找到的第一个
=> 返回值: 替换好的字符串
var str = '你好 世界 H H abc abc HH 你好'
var res = str.replace('HH','好好学习')
console.log(res);
8. concat()
=> 语法: 字符串.concat(字符串) => 作用: 拼接字符串 => 返回值: 拼接好的字符串
var str = 'hello'
var res = str.concat('world')
console.log(res);
9. slice()
=> 语法: 字符串.slice(开始索引,结束索引) - 包前不包后 -> 和substring的区别就是可以写 负整数 -> 当你写负整数的时候,表示 length + 负整数 => 作用: 截取字符串 => 返回值:截取好的字符串
var str = 'hello world'
var res = str.slice(2,5)
var res = str.slice(2,-3)
console.log(res);
10. split()
=> 语法: 字符串.split('切割符号',多少个)
-> 切割符号,按照你写的符号把字符串切割开
+ 如果不写,那么就是切割一个完整的
+ 如果写一个空字符串(''),按照一位一位的切割
-> 多少个,选填,默认是全部,表示你切割完以后保留多少个
=> 返回值: 一个数组的形式保存每一段内容
-> 不管按照什么切割,返回值一定是一个数组
var str = '2022-12-12'
var res = str.split('-')
console.log(res);
var res = str.split('-',1)
console.log(res);
var res = str.split()
//+ 如果不写,那么就是切割一个完整的
console.log(res);
var res = str.split('')
//如果写一个空字符串(''),按照一位一位的切割
console.log(res);
11. indexof()
=> 语法:
1. 字符串.indexof(字符串片段)
2. 字符串.indexof(字符串片段,开始索引)
=> 作用:再字符串里面查找指定字符串片段
=> 返回值:
-> 如果查询到了,就是指定索引
-> 如果没有,就是 -1
12. lastIndexOf()
=> 语法:
1.字符串.lastIndexOf(字符串片段)
2.字符串.lastIndexOf(字符串片段,开始索引)
=> 作用:再字符串里面查找指定字符串片段
=> 返回值:
-> 如果查询到了,就是指定索引
-> 如果没有,就是 -1
var str = 'abcaabbcc'
var res = str.lastIndexOf('a')
console.log(res);
var res = str.lastIndexOf('a',2)
console.log(res);
var res = str.lastIndexOf('aa')
console.log(res);
var res = str.lastIndexOf('z')
console.log(res);
13. includes()
=> 语法:字符串.includes('字符串片段')
=> 作用:字符串里面是否包含该字符串片段
=> 返回值:布尔值
-> 有就是 true
-> 没有就是false
var str = 'hello world'
var res = str.includes('a')
console.log(res);
var res = str.includes('l')
console.log(res);
14. search()
=> 语法:字符串.search('字符串片段')
=> 作用: 查找字符串里面有没有匹配的字符串片段
=> 返回值:
-> 如果有,就是指定索引
-> 如果没有,就是 -1
=> 和 indexOf 的区别
1.没有第二个参数
2.search 参数可以写正则
var str = 'hello world'
var res = str.search('l')
console.log(res);
var res = str.search('a')
console.log(res);
15. match()
=> 语法:字符串.match('字符串片段')
=> 作用:找到字符串里面的字符串片段
=> 返回值:是一个数组
->里面是找到的字符串片段
=> 实际应用:
-> 不是传递字符串
-> 传递正则
var str = 'hello world'
var res = str.match('o')
console.log(res);
16. trim()
=> 语法:字符串.trim() => 作用:去除首尾空格 => 返回值: 去除空格以后的字符串
var str = ' 你好 世界 '
var res = str.trim()
console.log(res);
console.log(str);
17. trimStart()
=> 语法:字符串.trimStart() => 作用: 去除开始的空格 => 返回值: 去除空格以后的字符串 => 别名:trimLeft()
var str = ' 你好 世界 '
// var res = str.trimStart()
var res = str.trimLeft()
console.log(res);
console.log(str);
18. trimEnd()
=> 语法:字符串.trimEnd() => 作用: 去除尾部的空格 =>返回值: 去除空格以后的字符串 => 别名:trimRight()
var str = ' 你好 世界 '
// var res = str.trimEnd()
var res = str.trimRight()
console.log(res);
console.log(str);
19. padStart()
=> 语法: 字符串.padStart(目标长度,'填充字符串')
-> 目标长度 : 你想把字符串补充到多长
+如果你写的长度小于字符串本身长度,那么这个函数没有意义
+超过长度以后用填充字符串补齐
-> 填充字符串:可以是一个字符,也可以是多个
=> 作用: 从前面字符串补齐
=> 返回值:补齐以后的字符串
var str = '1234'
var res = str.padStart(3,'a')
console.log(res);
//前面6个全部是a
var res = str.padStart(10,'a')
console.log(res);
//根据补位剩余的空间,来使用字符补位
var res = str.padStart(5,'abcd')
console.log(res);
20. padEnd()
=> 语法: 字符串.padEnd(目标长度,'填充字符串')
-> 目标长度 : 你想把字符串补充到多长
+如果你写的长度小于字符串本身长度,那么这个函数没有意义
+超过长度以后用填充字符串补齐
-> 填充字符串:可以是一个字符,也可以是多个
=> 作用: 从后面字符串补齐
=> 返回值:补齐以后的字符串
var str = '1234'
var res = str.padEnd(3,'a')
console.log(res);
var res = str.padEnd(10,'a')
console.log(res);
var res = str.padEnd(5,'abc')
console.log(res);
21. startsWith()
=> 语法:字符串.startsWith('字符串片段')
=> 作用:判断该字符串是不是以这个字符串片段开始
=> 返回值:一个布尔值
-> 如果是,就是true
-> 如果不是,就是false
var str = 'hello world 你好 世界'
var res = str.startsWith('hello')
console.log(res);
22. endsWith()
=> 语法:字符串.EndsWith()('字符串片段')
=> 作用:判断该字符串是不是以这个字符串片段结尾
=> 返回值:一个布尔值
-> 如果是,就是true
-> 如果不是,就是false
var str = 'hello world 你好 世界'
var res = str.endsWith('hello')
console.log(res);
二、鸡肋的方法 (食之无味,弃之可惜)
1.small()
=> 语法: 字符串.small()
=> 作用:把字符串里面的内容变成小号文字
=> 返回值: <small>字符串</small>
var str = 'hello world'
var res1 = str.small()
document.write(res1);
document.write('<br>')
document.write(str)
document.write('<br>')
2.big()
=>语法: 字符串.big()
=> 作用:把字符串里面的内容变成大号文字
=> 返回值: <big>字符串</big>
var str = 'hello world'
var res2 = str.big()
document.write(res2);
document.write('<br>')
document.write(str)
3.bold()
=> 语法: 字符串.bold()
=> 作用:把字符串加粗显示
=> 返回值:<bold>字符串</bold>
var str = 'hello world'
var res3 = str.bold()
document.write(res3);
document.write('<br>')
document.write(str)
4. fontSize()
=> 语法: 字符串.fontSize(尺寸)
=> 作用: 指定字符串大小
=> 返回值: <fontSize>字符串</fontSize>
var str = 'hello world'
var res4 = str.fontsize(50)
document.write(res4);
document.write('<br>')
document.write(str);
5. fontcolor()
=> 语法: 字符串.fontcolor(颜色) => 作用: 指定字符串颜色 => 返回值: 第一个颜色行内样式
var str = 'hello world'
var res5 = str.fontcolor('pink')
document.write(res5);
document.write('<br>')
document.write(str);