文章目录
- 1. 创建字符串
- 2. 字符串操作方法
- 拼接字符串 concat()
- 提取子字符串 slice()、substr()、substring()
- 3. 字符串位置方法
- 定位子字符串 indexof()、 lastIndexOf()
- 4. 字符串包含方法
- startsWith()、endsWith()、includes()
- 5. trim()方法
- 6. repeat()方法
- 7. padStart()和padEnd()方法
- 8. 字符串迭代与解构
- @@iterator方法
- 9. 字符串大小写转换
- 10. 字符串模式匹配方法
- match()
- search()
- replace()
- split()
String 是我们常用的一种数据类型
String类型提供了很多方法来解析和操作字符串
1. 创建字符串
let str = new String("hello");
let str = "hello";
- 有3种方法可以返回 对象的原始字符串值
valueOf()、toLcaleString()、toString() - 每个String对象都有一个length属性,表示字符串中字符的数量。
2. 字符串操作方法
拼接字符串 concat()
参数:concat()方法可以接收任意多的参数,因此可以一次性拼接多个字符串
let str = "hello ";
let result = str.concat("world", "!");
console.log(result); // "hello world!"
虽然此方法可以拼接字符串,但更常用的方法是使用加号操作符。且多数情况下使用加号更方便。
提取子字符串 slice()、substr()、substring()
- slice()、substring()
参数:①子字符串开始位置,②子字符串结束位置(可选)
返回:一个新字符串 - substr()
参数:①子字符串开始位置,②返回子字符串的长度(可选)
返回:一个新字符串
结束位置是指会提取此位置之前的字符串。
三个方法省略第二个参数都意味着提取到字符串末尾。
let str = "hello world";
let str1 = str.slice(3); // "lo world"
let str2 = str.substr(3); // "lo world"
let str3 = str.substring(3); // "lo world"
let str4 = str.slice(3,7); // "lo w"
let str5 = str.substr(3,7); // "lo worl"
let str6 = str.substring(3,7); // "lo w"
如果传入负数作为参数
let str = "hello world";
let str1 = str.slice(-3); // "rld" 参数转换为字符串长度+负参数值
let str2 = str.substr(-3); // "rld" 参数转换为字符串长度+负参数值
let str3 = str.substring(-3); // "hello world" 参数转换为0
// 等价于(3,7)
let str4 = str.slice(3,-4); // "lo w"
// 等价于(3,0)
let str5 = str.substr(3,-4); // ""
// 等价于(0,3) 第二个参数转换为0,但会将小参数放在前面
let str6 = str.substring(3,-4); // "lo w"
3. 字符串位置方法
定位子字符串 indexof()、 lastIndexOf()
参数:①搜索的字符串,②开始搜索位置(可选)
返回:位置(没找到返回-1)
如果传入第二个参数,indexOf从指定位置(包括此位置)向末尾搜索、lastIndexOf从指定位置向开头搜索。
let str = "hello world";
let n1 = str.indexOf("o"); // 4
let n2 = str.lastIndexOf("o"); // 7
使用第二个参数并循环调用indexOf或lastIndexOf方法,可以找到所有的子字符串。
let str = "good morning, and in case i don't see you, good afternoon, good evening, and good night!";
let index = str.indexOf("oo");
const positions = [];
while(index > -1){
positions.push(index);
index = str.indexOf("oo", index+1);
}
console.log(positions); // [1, 44, 54, 60, 78]
4. 字符串包含方法
startsWith()、endsWith()、includes()
- startsWith()、includes()
参数:①字符串,②开始搜索的位置(可选)
返回:一个布尔值 - endsWith()
参数:①字符串,②应该当作字符串末尾的位置(可选)
返回:一个布尔值
let msg = "foobarbaz";
// startsWith 检查开始于0的索引
let res1 = msg.startsWith("foo"); // true
let res2 = msg.startsWith("bar"); // false
// endsWith 检查开始于(string.length - substring.length)的索引
let res3 = msg.startsWith("baz"); // true
let res4 = msg.startsWith("bar"); // false
// includes检查整个字符串
let res5 = msg.startsWith("oo"); // true
startsWith、includes接收第二个参数意味着从指定位置向字符串末尾搜索
endsWith接收第二个参数表示将此参数当作字符串的长度
let msg = "foobarbaz";
let res1 = msg.endsWith("ba"); // false
let res2 = msg.endsWith("ba", 8); // true 提供了第二个参数相当于搜索的字符串变成了"foobarba"长度为8。
5. trim()方法
删除前后空格,返回一个新字符串
let str = " hello world ";
let trimStr = str.trim(); // "hello world"
另外 trimLeft() 和 trimRight()方法分别用于从字符串开始和末尾清理空格。
6. repeat()方法
参数:①一个整数(表示复制多少次)
返回:拼接所有副本后的结果
let str = "na ";
let strCopy = str.repeat(10); // "na na na na na na na na na na "
7. padStart()和padEnd()方法
复制字符串,如果小于指定长度,则在相应一边填充字符。
参数:①长度,②填充字符串(可选)
返回:位置(没找到返回-1)
let str = "foo";
let res1 = str.paStart(6); // " foo" 无第二个参数,默认为空格
let res2 = str.padEnd(6); // "foo "
如果提供了多个字符,则会将其拼接并截断以匹配指定长度。
let str = "foo";
let str1 = str.padStart(8, "barbaz"); // "barbafoo"
let str2 = str.padEnd(8, "barbaz"); // "foobarba"
8. 字符串迭代与解构
@@iterator方法
此方法表示可以迭代字符串的每个字符
手动使用迭代器
let msg = "abc";
let msgIter = msg[Symbol.iterator]();
console.log(msgIter.next()); // {value: 'a', done: false}
console.log(msgIter.next()); // {value: 'b', done: false}
console.log(msgIter.next()); // {value: 'c', done: false}
console.log(msgIter.next()); // {value: undefined, done: true}
for of 循环中通过迭代器访问每个字符
for(let c of "abcde"){
console.log(c);
}
有了迭代器之后,字符串就可以通过解构操作符来解构。
// 便的把字符串分割为字符数组
let msg = "abcde";
let res = [...msg]; // ["a","b","c","d","e"]方
9. 字符串大小写转换
toLowerCase()、toLocaleLowerCase()
toUpperCase()、toLocaleUpperCase()
let str = "Hello World";
let str1 = str.toLowerCase(); // "hello world"
let str2 = str.toLocaleLowerCase(); // "hello world"
let str3 = str.toUpperCase(); // "HELLO WORLD"
let str4 = str.toLocaleUpperCase(); // "HELLO WORLD"
10. 字符串模式匹配方法
match()
参数:①正则表达式字符串或RegExp对象
返回:数组
let text = "cat,bat,sat,fat";
let matches = text.match(/at/);
// ['at', index: 1, input: 'cat,bat,sat,fat', groups: undefined]
search()
参数:①正则表达式字符串或RegExp对象
返回:第一个匹配的位置索引(没有返回-1)
let text = "cat,bat,sat,fat";
let index = text.search(/at/); // 1
replace()
参数:①RegExp对象或字符串,②一个字符串或一个函数
返回:字符串
如果第一个参数是字符串,那么只替换第一个子字符串。想要替换所有子字符串,第一个参数必须为正则表达式并且带全局标记。
let text = "cat,bat,sat,fat";
let res1 = text.replace("at", "oo"); // "coo,bat,sat,fat"
let res2 = text.replace(/at/g, "oo"); // "coo,boo,soo,foo"
split()
参数:①RegExp对象或字符串,②数组大小(可选)
返回:数组
根据传入的分隔符将字符串拆分成数组。
第二个参数表示返回的数组不会超过此大小。
let colorText = "red,blue,green,yellow";
let colors1 = colorText.split(","); // ["red","blue","green","yellow"]
let colors2 = colorText.split(",", 2); // ["red", "blue"]