String 字符串
一、创建字符串
1.字面量
let str1 = 'hello!早上好!'
2.new 操作符 new String();
let str2 = new String('你好哇!')
console.log(str1)
console.log(str2)
二、获取字符串的值
1.通过[索引值]获取字符
console.log(str1[0])
console.log(str2[1])
2.charAt(索引)
console.log(str1.charAt(2))
console.log(str2.charAt(3))
超出范围查找
console.log(str2[10]) //undefined
console.log(str2.charAt(10)) //"" 推荐使用
三、String 字符串方法
1.concat()
2.indexOf()
3.lastIndexOf()
4.replace
5.repeat()
6.toUpperCase()
7.toLowerCase()
8.slice()
9.substring()
10.substr()
11.split()
12.includes()
13.match()
14.search()
ps:都不会修改原字符串
支持正则表达式的 String 对象的方法:
search 检索与正则表达式相匹配的值。
match 找到一个或多个正则表达式的匹配。
replace 替换与正则表达式匹配的子串。
split 把字符串分割为字符串数组。
1.concat()
console.log(str1.concat(str2))
console.log(str1.concat(str2[2]))
console.log(str1.concat('O(∩_∩)O哈哈~', str2[2]))
2.indexOf()
3.lastIndexOf()
注意: 和字符串不一样的是,第二个参数不支持负数
//查找最后一个 l
console.log(str1.lastIndexOf('l')) //4
// 查找第一个l的下标
console.log(str1.lastIndexOf('l', 2)) //3 从下标2开始往前面找
4.replace()
该方法用于字符串中某些字符替换换另一些字符 参数:
第一个参数:需要替换的值(支持正则)
第二个参数 替换的新值
let str3 = '今天是什么鬼天气'
console.log(str3.replace('鬼', '好')) //今天是什么好天气
console.log(str3) //不会修改原字符
let str4 = '今天是什么鬼天气,好鬼热'
console.log(str4.replace('鬼', '好')) //今天是什么好天气,好鬼热
让每个都被替换
for (let i = 0; i < str4.length; i++) {
if (str4.charAt(i) == '鬼') {
str4.replace(str4.charAt(i), '好') //原数组不会被修改,需要重新赋值
str4 = str4.replace(str4.charAt(i), '好') //重新赋值
}
}
使用正则让每个都被替换
str4 = str4.replace(/鬼/g, '好')
// console.log(str4.replaceAll(/鬼/,"好"));
console.log(str4)
5.repeat()
将原字符串重复 n 次,返回一个新的字符串
let str5 = 'hello!'
console.log(str5.repeat()) //"" 默认为0
console.log(str5.repeat(3)) //hello!hello!hello!
6.toUpperCase()
字符串大写转换使用函数
7.toLowerCase()
字符串小写转换使用函数
let jsStr = 'JAVASCRIPT'
jsStr = jsStr.toLowerCase()
console.log(jsStr) //javascript
jsStr = jsStr.toLowerCase().toUpperCase()
console.log(jsStr) //JAVASCRIPT
8.slice
9.substring()
提取字符串中两个指定索引号之间的字符 和 slice()类似,不同的是,substring 不支持负数、写负数会返回 ""
let str6 = ' 快三点钟了,饮茶啦'
console.log(str6.substring(1, 4)) //快三点
console.log(str6.substring(7)) //饮茶啦
写负数
console.log(str6.slice(-3)) //饮茶啦
console.log(str6.substring(-3)) // 快三点钟了,饮茶啦
截取 "饮茶"
console.log(str6.substring(7, 9)) //饮茶
console.log(str6.substring(-3, -1)) //""
console.log(str6.substring(-1, -1)) //""
10.substr()
从起始索引号提取字符串中指定的字符参数 参数: 第一个参数 开始下标(必选) 可以写负数 第二个参数 截取的长度(可选) 和 splice()不同的是,substr()不影响原字符串
11.split()
字符串以规定的格式分割成数组(字符串转数组) 参数:
第一个参数规定的格式
第二个参数分割的长度(可选)
如果不填,那么整个字符串都会被分割
注意: 不会影响原字符串
let str7 = '字符串--转数组的方法'
console.log(str7.split('--')) //[ '字符串', '转数组的方法' ]
console.log(str7.split()) // [ '字符串-转数组的方法' ]
console.log(str7.split('')) //['字', '符', '串','-', '转', '数','组', '的', '方','法']
12.includes()
ES6 在 String 原型上新增了 includes()方法,用于取代传统的只能用 indexOf 查找包含字符的方法(indexOf 返回-1 表示没查到不如 includes 方法返回 false 更明确,语义更清晰)
此外还新增了 startsWith(), endsWith(), padStart(),padEnd(),repeat()等方法,可方便的用于查找,补全字符串。
13 .match()
在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
检测方法:
str.match(regexp)
参数:
regexp 规定要匹配模式的RgeExp对象(需要匹配正则)
返回:
在字符串内检索指定的值,返回找到一个或多个正则表达式的匹配
var str14 = 'The rain in SPAIN stays mainly in the plain'
console.log(str14.match(/ain/g)) //[ 'ain', 'ain', 'ain' ]