常用的字符串方法
- 语法: 字符串.方法()
- 注意: 所有的字符串方法不会修改原字符串内容, 都是以返回值的形式得到结果
1. charAt()
- 语法: 字符串.charAt(索引)
- 返回值: 返回索引位置对应的字符
+ 如果索引不存在则返回'' 空字符串
1. charAt
console.log( str.charAt(1) ) // 'w'
console.log( str.charAt(10) ) // ''
2. charCodeAt()
- 语法: 字符串.charCodeAt(索引)
- 返回值: 返回索引位置对应字符 的 Unicode编码值
+ 如果索引不存在则返回 NaN
2. charCodeAt
console.log( str.charCodeAt(1) ) // 119 == >'w'
console.log( str.charCodeAt(10) ) // NaN
3. indexof()
- 语法1 : 字符串.indexOf(字符片段)
- 返回值: 返回传入 字符片段 在字符串中的 索引位置
+ 在字符串中,从前往后检索,找到了就停止
+ 如果 传入 字符片段 在字符串中 则返回 -1
3. indexOf
console.log( str.indexOf('e') ) // 2
console.log( str.indexOf('w') ) // 1
console.log( str.indexOf('z') ) // -1
- 语法2 : 字符串.indexof(字符片段,开始索引)
- 返回值: 返回传入 字符片段 在字符串中的 索引位置
+ 在字符串中,从开始索引位置,从前往后检索,找到了就停止
+ 如果 传入 字符片段 在字符串中 则返回 -1
console.log( str.indexOf('q',3) ) // 5
4. lastIndexOf()
- 语法1 : 字符串.lastIndexOf(字符片段)
- 返回值: 返回传入 字符片段 在字符串中的 索引位置
+ 在字符串中,从后往前检索,找到了就停止
+ 如果 传入 字符片段 在字符串中 则返回 -1
4. lastIndexOf
console.log( str.lastIndexOf('e') ) // 2
console.log( str.lastIndexOf('z') ) // -1
console.log( str.lastIndexOf('q') ) // 5
- 语法2 : 字符串.lastIndexOf(字符片段,开始索引)
- 返回值: 返回传入 字符片段 在字符串中的 索引位置
+ 在字符串中,从开始索引位置,从后往前检索,找到了就停止
+ 如果 传入 字符片段 在字符串中 则返回 -1
console.log( str.lastIndexOf('q',3) ) // 0
console.log( str.lastIndexOf('ert') ) // 2
5. includes()
- 语法: 字符串.includes(字符片段)
+ 返回值: 传入 字符片段 在字符串中存在则返回 true,否则false
5. includes
console.log( str.includes('qq') ) // false
console.log( str.includes('tq') ) // true
console.log( str.includes('tq',10) ) // false
从str字符串的索引10位置开始检索查找
6. toLowerCase() / toUpperCase()
- 语法: 字符串.toLowerCase()
- 语法: 字符串.toUpperCase()
+ 返回值: 字符串转为大写或小写后的内容
var str = 'qwSerStqAsAf';
6. toLowerCase/toUppercase
console.log( str.toLowerCase() ) // 'qwserstqasaf'
console.log( str.toUpperCase() ) // 'QWSERSTQASAF'
7. split()
- 语法: 字符串.split(分隔符)
+ 返回值: 一个数组,数组中的数据是字符串按照传入 分隔符 将字符串进行分隔后的内容
- 注意: 如果不传入分隔符或 分隔符在字符串中不存在,则整个字符串会当做一个数组数据
7. split
var str = '2022-11-23';
console.log( str.split('-') ) // ["2022", "11", "23"]
console.log( str.split('***') ) // ["2022-11-23"]
console.log( str.split() ) // ["2022-11-23"]
console.log( str.split('') )
["2", "0", "2", "2", "-", "1", "1", "-", "2", "3"]
8. substr
- 语法: 字符串.substr(开始索引,截取个数)
+ 返回值: 从字符串中截取的 字符串片段
8. substr
console.log( str.substr(2,2) ) // 'er'
9. substring
- 语法: 字符串.substring(开始索引,结束索引)
+ 注意: 截取的时候 包含开始 不包含结束
+ 返回值: 从字符串中截取的 字符串片段
9. substring
console.log( str.substring(2,5) ) // 'ert'
10. slice
- 语法: 字符串.slice(开始索引,结束索引)
+ 注意: 截取的时候 包含开始 不包含结束
- 结束索引可以写 负数; -1 表示最后一个字符 ...
+ 返回值: 从字符串中截取的 字符串片段
10. slice
console.log( str.slice(2,5) ) // 'ert'
console.log( str.slice(2,-1) ) // 'ertasd'
11. concat
- 语法: 字符串.concat(字符片段)
+ 字符串拼接
+ 返回值: 拼接后的字符串
11. concat
var str = 'hello'
console.log( str.concat(' world') ) // 'hello world'
12. replace
- 语法: 字符串.replace(替换下的内容,替换上的内容)
+ 注意: 只会替换第一个找到的内容
+ 返回值: 替换好的 字符串
12. replace
var str = 'rewQQtre'
console.log( str.replace('QQ','WX') ) // 'rewWXtre'
var str = 'rewQQtrQQe'
console.log( str.replace('QQ','WX') ) // 'rewWXtrQQe'
13. startsWith
- 语法: 字符串.startsWith(字符串片段)
+ 判断字符串 是否是以指定内容开始
+ 返回值: 布尔值
var str = 'rewQQtrQQe'
13. startsWith
console.log( str.startsWith('req') ) // fals
console.log( str.startsWith('rew') ) // true
14. endsWith
- 语法: 字符串.endsWith(字符串片段)
+ 判断字符串 是否是以指定内容开始
+ 返回值: 布尔值
var str = 'rewQQtrQQe'
14. endsWith
console.log( str.endsWith('QQ') ) // false
console.log( str.endsWith('QQe') ) // true
15. trim
- 语法: 字符串.trim()
+ 将字符串两边的空白内容去除
+ 返回值: 去除两边空白内容的字符串
var str = ' wqre rtrtr '
15. trim
console.log( `--${str}--` )
console.log( `--${str.trim()}--` )
16. trimRight / trimEnd
- 语法: 字符串.trimRight()
- 语法: 字符串.trimend()
+ 将字符串结尾的空白内容去除
+ 返回值: 去除结尾空白内容的字符串
var str = ' wqre rtrtr '
16. trimRight/trimEnd
console.log( `--${str}--` )
console.log( `--${str.trimRight()}--` )
console.log( `--${str.trimEnd()}--` )
17. trimLeft / trimStart
- 语法: 字符串.trimStart()
- 语法: 字符串.trimLeft()
+ 将字符串开头的空白内容去除
+ 返回值: 去除开头空白内容的字符串
var str = ' wqre rtrtr '
17. trimLeft/trimStart
console.log( `--${str}--` )
console.log( `--${str.trimLeft()}--` )
console.log( `--${str.trimStart()}--` )