字符串及数组常用方法总结 | 青训营笔记

70 阅读3分钟

这是我参与「第四届青训营 」笔记创作活动的的第3天

字符串常用方法总结

//1.
str.length//2.str.charAt(num)     通过索引查找值
let message = "abcde";
console.log(message.charAt(2)); // "c"//3.str.charCodeAt(num);
let message = "abcde";
// Unicode "Latin small letter C"的编码是U+0063,即99(十进制)
console.log(message.charCodeAt(2)); // 99//4.str.concat()
//将一个或多个字符串拼接成一个新字符串
let stringValue = "hello ";
let result = stringValue.concat("world");//可以传多个参数
console.log(result); // "hello world"
console.log(stringValue); // "hello"//5.slice和substring的第二个参数是结尾的索引,substr的第二个参数是截取的个数,参数可以为负值
let stringValue = "hello world";
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.substring(3)); // "lo world"
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w"
console.log(stringValue.substring(3,7)); // "lo w"
console.log(stringValue.substr(3, 7)); // "lo worl"//6.位置方法:indexOf() 从开头查找和lastIndexOf() 从结尾查找
let stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4
console.log(stringValue.lastIndexOf("o")); // 7//7.包含方法: startsWith() 、 endsWith() 和 includes(),可以接受第二个参数
let message = "foobarbaz";
console.log(message.startsWith("foo")); //true
console.log(message.startsWith("foo", 1)); //false
console.log(message.includes("bar")); //true
console.log(message.includes("bar", 4)); //false
console.log(message.endsWith("bar")); //false
console.log(message.endsWith("bar", 6)); // true//8. trim() 方法。这个方法会创建字符串的一个副本,删除前、后所有空格符//9.repeat() 复制
let stringValue = "na ";
console.log(stringValue.repeat(16) + "batman");
// na na na na na na na na na na na na na na na na batman//10. for-of 循环//11.解构操作符
let message = "abcde";
console.log([...message]); // ["a", "b", "c", "d", "e"]//12.大小写替换 toLowerCase() 、toUpperCase()//13.正则表达式匹配 text.match(pattern)//14.str.replace(str1,str2)//15.str.split(str1) 第二个参数是数组大小
let colorText = "red,blue,green,yellow";
let colors1 = colorText.split(","); //["red", "blue", "green", "yellow"]
let colors2 = colorText.split(",", 2); //["red", "blue"]//16.localeCompare() 方法 ,比较两个字符串
let stringValue = "yellow";
console.log(stringValue.localeCompare("brick"));
// 1
console.log(stringValue.localeCompare("yellow"));
// 0
console.log(stringValue.localeCompare("zoo"));
// -1
​
​

六、数组常用方法

//1. 复制和填充 arr.fill()
const zeroes = [0, 0, 0, 0, 0];
// 用5填充整个数组
zeroes.fill(5);
console.log(zeroes); // [5, 5, 5, 5, 5]
zeroes.fill(0); // 重置
// 用6填充索引大于等于3的元素
zeroes.fill(6, 3);
console.log(zeroes); // [0, 0, 0, 6, 6]
zeroes.fill(0); // 重置
// 用7填充索引大于等于1且小于3的元素
zeroes.fill(7, 1, 3);
console.log(zeroes); // [0, 7, 7, 0, 0];
zeroes.fill(0); // 重置//2.toString()和 valueOf()方法 它们分别返回了数组的字符串表示,即将所有字符串组合起来,以逗号分隔//3. join() 方法,接收一个参数,即字符串分隔符
let colors = ["red", "green", "blue"];
alert(colors.join(",")); // red,green,blue
alert(colors.join("||")); // red||green||blue//4.栈方法:push和pop方法 
//push返回数组的最新长度,pop() 方法则用于删除数组的最后一项
let count = colors.push("red", "green");
​
//5.队列方法:shift()它会删除数组的第一项并返回它
//使用shift() 和 push() ,可以把数组当成队列来使用
//unshift() 和 pop()//6.排序方法: reverse()反向排列和sort()//7. concat() 方法
let colors = ["red", "green", "blue"];
let colors2 = colors.concat("yellow", ["black",
"brown"]);
console.log(colors); // ["red", "green","blue"]
console.log(colors2); // ["red", "green", "blue","yellow", "black", "brown"]//8. slice() 方法,可以接收一个或两个参数
let colors = ["red", "green", "blue", "yellow","purple"];
let colors2 = colors.slice(1);
let colors3 = colors.slice(1, 4);
alert(colors2); // green,blue,yellow,purple
alert(colors3); // green,blue,yellow//9.splice()
//删除: splice() 传2个参数:要删除的第一个元素的位置和要删除的元素数量
//插入: splice() 传3个参数:开始位置、0(要删除的元素数量)和要插入的元素,如 splice(2, 0, "red", "green")
//替换:splice()传入3个参数:开始位置、要删除元素的数量和要插入的任意多个元素,如splice(2, 1, "red", "green") 
let colors = ["red", "green", "blue"];
let removed = colors.splice(0,1); // 删除第一项
alert(colors); // green,blue
alert(removed); // red,只有一个元素的数组
removed = colors.splice(1, 0, "yellow", "orange");
// 在位置1插入两个元素
alert(colors);// green,yellow,orange,blue
alert(removed);// 空数组
removed = colors.splice(1, 1, "red", "purple"); //插入两个值,删除一个元素
alert(colors); //green,red,purple,orange,blue
alert(removed); //yellow,只有一个元素的数组//10.搜索和位置方法:indexOf() 、lastIndexOf() 和 includes()
//indexOf() 、lastIndexOf():返回要查找的元素在数组中的位置,如果没找到则返回-1
//includes() 返回布尔值,表示是否至少找到一个与指定元素匹配的项
let numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1];
alert(numbers.indexOf(4)); // 3
alert(numbers.lastIndexOf(4)); // 5
alert(numbers.includes(4)); // true
//可以接收第二个参数,表示从该位置开始搜索
alert(numbers.indexOf(4, 4)); // 5
alert(numbers.lastIndexOf(4, 4)); // 3
alert(numbers.includes(4, 7)); // false

\