let str = 'hello world';
let str1 = "hello";
let str2 = "hello1";
let str3 = 'hello WORLD';
let str4 = ' hello world ';
//charAt(index) 将字符串视为字符数组。它检索你提供的索引处的字符
console.log(str.charAt(0),str.charAt(1),str.charAt(2)); // 'h' 'e' 'l'
//includes(string) 检查字符串是否包含子字符串
console.log(str.includes('hello'), str.includes('word')); // true false
//match(regex string) 字符串是否与正则表达式匹配
const nameRegex = /^[a-zA-Z]+$/
console.log(str1.match(nameRegex), str2.match(nameRegex)); // true false
//replace(str,str1) 获取字符串中出现的字符并将其替换为另一个字符
console.log(str.replace('l','L'), str.replace(/l/g,"L")); // 'heLlo world''heLLo worLd'
//split(string) 根据某个字符或其他字符串拆分字符串
console.log(str.split(' ')); // ['hello', 'world']
//substring(index, index) 索引范围拆分字符串
console.log(str.substring(1, 4), str); // 'ell' 'hello world'
//substr(n,m) 从索引n开始,截取m个字符
console.log(str.substr(1, 4), str) // 'ello' 'hello world'
//slice(n,m) 从索引n开始,截取到索引m,不包括m
console.log(str.slice(1, 4), str) // 'ell' 'hello world'
//toLowerCase()/toUpperCase() 将字符串转换为全部大写或全部小写
console.log(str3.toUpperCase(),str3.toLowerCase()); //'HELLO WORLD' 'hello world'
//trim() 从字符串中删除头尾空格
console.log(str4.trim()) // 'hello world'
//indexOf()/lastIndexOf()查询元素位置
console.log(str.indexOf('o'), str.lastIndexOf('o')) // 4 7
//push 向数组尾部添加元素
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.push(8,9);
console.log(a,array); //9 [1,2,3,4,5,6,7,8,9]
//unshift 向数组头部添加元素
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.unshift(8,9);
console.log(a,array); // 9 [8,9,1,2,3,4,5,6,7]
//pop 从数组尾部删除一个元素
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.pop();
console.log(a,array); // 7 [1,2,3,4,5,6]
//shift 从数组头部删除一个元素
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.shift();
console.log(a,array); // 1 [2,3,4,5,6,7]
//slice 不改变原数组的截取
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.slice(1,3);
console.log(a, array); // [2,3] [1,2,3,4,5,6,7]
//splice 改变原数组的截取
let array = [1, 2, 3, 4, 5, 6, 7];
let a = array.splice(1,3);
console.log(a, array); // [2,3,4] [1,5,6,7]
//reverse 数组倒序
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.reverse()); // [7,6,5,4,3,2,1]
//sort 数组排序
let array = [1, 2, 3, 4, 5, 6, 7];
array.sort((a,b)=>{
return b - a; // 从大到小 return a - b;从小到大
})
console.log(array); // [7,6,5,4,3,2,1]
//join 数组按照指定分隔符转换成字符串
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.join('-')); // '1-2-3-4-5-6-7'
//concat 数组拼接
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.concat([8,9])); // [1,2,3,4,5,6,7,8,9]
//indexOf/lastIndexOf查询元素位置
let array = [1, 2, 3, 4, 5, 6, 7,1];
console.log(array.indexOf(1), array.lastIndexOf(1)) // 0 7
//Array.from 将类数组对象或可迭代对象转化为数组,比如:arguments,js选择器找到的dom集合
<div class="number">1</div>
<div class="number">2</div>
let domArray = document.getElementsByClassName('number')
console.log(domArray, Array.from(domArray))
//HTMLCollection(2)[div.number, div.number] [div.number, div.number]
//includes(value: 搜索值,stratIndex:搜索的起始位置) 检测数组中是否包含一个值
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.includes(1), array.includes(1,1)); //true false
//fill(value,startIndex,endIndex) 将一定范围索引的数组元素内容填充为单个指定的值
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.fill(0,1,2)); // [1,0,3,4,5,6,7]
//flat(num: 嵌套层数) 嵌套数组转一维数组
let array = [1, [2, [3, ,4], 5], 6, 7];
console.log(array.flat()); //[1,2,[3,4],5,6,7]
console.log(array.flat(2)); //[1, 2, 3, 4, 5, 6, 7]
console.log(array.flat(Infinity)); //[1, 2, 3, 4, 5, 6, 7] Infinity不管有多少层执行到底
拓展:
1.数组去重,取重
let array = [1,1,2,2,3,3,4,5,6]
//去重
let newArray = [...new Set(array)]
//取重
let compArray = array.filter((el,index)=>
((array.indexOf(el) != array.lastIndexOf(el)) && (array.indexOf(el) == index))
)
console.log(newArray,compArray)// [1, 2, 3, 4, 5, 6] [1, 2, 3]
js字符串,数组操作大全
于 2023-06-07 10:20:05 首次发布
本文介绍了JavaScript中一系列重要的字符串和数组操作方法,如charAt、includes、match、replace、split等字符串方法,以及push、unshift、pop、shift、slice、splice等数组方法。还涵盖了数组的排序、合并、去重等高级操作,帮助开发者更好地理解和使用这些功能。
摘要由CSDN通过智能技术生成