javascript常见字符串操作和数组操作

1.字符串操作
常用的方法有:
charAt()、charCodeAt()、concat()、 slice()、 substr()、 substring()、 indexOf()、 lastIndexOf()、 trim()、 toLowerCase()、 toLocaleLowerCase()、 toUpperCase()、 toLocaleUpperCase()。
与正则有关的方法有:
match(),search(),replace()、split()

// charAt(),charCodeAt()这两个方法分别返回给定位置的字符和字符编码
var chart_str = 'hello world!';
chart_str.charAt(0);//'h'
chart_str.charCodeAt(0);//104
chart_str.charAt(14);//'',越界会返回空字符
chart_str.charCodeAt(0);//NaN
//concat()字符串连接操作
var concat_str = 'hello';
concat_str.concat('!');//'hello !'返回连接后的新的字符串,等价字符串+操作
concat_str;//'hello' 做concat操作后,源不会改变
concat_str.concat(' ', 'world', '!');//"hello world!"可接收多个参数
concat_str.concat([1,2,3]);//"hello1,2,3",传入对象,会调用toString()方法后在操作
//indexOf(),lastIndexOf()返回给定字符串所在的开始位置,可传入两个参数,第一个参数表示要查询的子串,第二参数表示从什么位置开始查,省略默认0,查到返回开始位置,否者-1
index_str= 'hello world!';
index_str.indexOf('hello');//0
index_str.lastIndexOf('hello');//0
index_str.indexOf('hello',0)//0
index_str.indexOf('hello',1);//-1
//trim()去除左右空格,等价replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,'')
var trim_str = ' hello  ';
trim_str.trim();//'hello'
trim_str.trimRight();//非标准
trim_str.trimLeft();//非标准
//toLowerCase()、 toLocaleLowerCase()、 toUpperCase()、 toLocaleUpperCase()大小写转换,toLocaleLowerCase()、 toLocaleUpperCase()针对于特殊地区,大部分情况和toLowerCase(),toUpperCase()结果一样
var case_str = 'hello';
case_str.toUpperCase();//'HELLO'
case_str.toUpperCase().toLowerCase();//'hello'
//split()字符串分割 
/**
E8 及之前版本会忽略捕获组。 ECMA-262 规定应该把捕获组拼接到结果数组中。 IE9 能正确地
在结果中包含捕获组。
Firefox 3.6 及之前版本在捕获组未找到匹配项时,会在结果数组中包含空字符串; ECMA-262 规
定没有匹配项的捕获组在结果数组中应该用 undefined 表示。
*/
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(","); //["red", "blue", "green", "yellow"]
var colors2 = colorText.split(",", 2); //["red", "blue"] 第二参数限制了项数
var colors3 = colorText.split(/[^\,]+/); //["", ",", ",", ",", ""]
//match()匹配 
var match_str = 'hello world! this is test';
var matchs = match_str.match(/.is/);//返回一个数组
matchs.length;//1
matchs[0];//'tis'
//search()搜索。search()方法返回字符串中第一个匹配项的索引;如果没
//有找到匹配项,则返回-1。
var search_text = "cat, bat, sat, fat";
text.search(/at/);//1
/**
replace()替换
es提供特殊字符序列
字符序列 替换字符
$$      $
$&      匹配整个模式的子字符串。与RegExp.lastMatch的值相同
$'      匹配的子字符串之前的子字符串。与RegExp.leftContext的值相同
$`      匹配的子字符串之后的子字符串。与RegExp.rightContext的值相同
$n      匹配第n个捕获组的子字符串,其中n等于0~9。例如, $1是匹配第一个捕获组的子字符串,
        $2是匹配第二个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串
$nn     匹配第nn个捕获组的子字符串,其中nn等于01~99。例如, $01是匹配第一个捕获组的子字符串, 
        $02是匹配第二个捕获组的子字符串,以此类推。如果正则表达式中没有定义捕获组,则使用空字符串
*/
var replacet_str = "cat, bat, sat, fat";
replacet_str.replace(/(.at)/g, "word ($1)");//"word (cat), word (bat), word (sat), word (fat)"
replacet_str.replace(/(.at)/g, "word ($$)");//"word ($), word ($), word ($), word ($)"
replacet_str.replace(/(.at)/g, "word ($')");//"word (, bat, sat, fat), word (, sat, fat), word (, fat), word ()"
replacet_str.replace(/(.at)/g, "word ($`)");//"word (), word (cat, ), word (cat, bat, ), word (cat, bat, sat, )"
/**
slice()、 substr()、 substring()
这三个方法都返回被操作字符串的子集的,不会改变被操作字符串,接受两个参数,第二参数可省略默认是字符的长度
第一个参数表示开始位置,第二个参数表示结束位置。substr()第二参数表示个数,而substring()和slice()第二个
参数表示子集最后一个字符的下个位置
*/
//slice()参数为负值时候会和字符串长度相加,参数规整后第一个参数必须小于第二个参数
var _str = 'hello world!';
_str.slice(2);//"llo world!"
_str.slice(-2);//"d!"
_str.slice(2,-7);//"llo"
_str.slice(-2,-7);//''
_str.slice(-7,7);//" w"
_str.slice(-7,-2);//" worl"
//substr() 第一个参数为负的时候与字符串长度求和,第二个参数为负置为0
_str.substr(2);//"llo world!"
_str.substr(-2);//"d!"
_str.substr(2,-7);//""
_str.substr(-2,-7);//''
_str.substr(-7,7);//" world!"
_str.substr(-7,-2);//" "
//substring() 参数为负置为0,参数大的作为结束位置,小的作为开始位置
_str.substring(2);//"llo world!"
_str.substring(-2);//"hello world!"
_str.substring(2,-7);//he"
_str.substring(-2,-7);//" "
_str.substring(-7,7);//"hello w"
_str.substring(-7,-2);//" "
//slice,substr,substring只传入一个正整数,结果相同
//slice,substr,substring只传入一个负整数,slice,substr结果相同,这两个方法第一个参数为
//负会和长度求和

2.数组操作
数组操作的方法有:
pop()、push()、shift()、unshift()、reverse()、sort()、concat()、slice()、splice()、indexOf()、lastIndexOf()、every()、filter()、forEach()、map()、some()、reduce()、reduceRight()

 var arr = [];
/**
*push,pop两个方法合用可以模拟栈结构(LIFO)
*push()入栈,
*pop()出栈
*/
arr.push(1);//1
arr.push(2);//2
arr.pop();//2
arr.pop();//1
arr;//[]
/**
*push,shift两个方法合用可以模拟队列结构(FIFO)
*push()入队,
*shift()出队从头部移除数据并返回,刚好和pop()相反
*/
arr.push(1);//1
arr.push(2);//2
arr.shift();//1
arr.shift();//2
arr;//[]
/**
*unshift()和shift()作用相反,unshift()在数组前面插入数据,而push()实在数据尾部添加数据
*unshift().push()返回Array的length
*/
arr.unshif(1);
arr.unshift(2);
arr;//[2,1]
arr.shift();//2
arr.shift();//1
arr.push(1);//1
arr.push(2);//2
arr;//[1,2]
/**
*数组的排序有两个方法reverse()反序,sort()可传入排序方法,
*默认升序排序,排序过程中调用每项的toString()方法来确定顺序
*/
arr.push('a',{x:2,y:3,toString:function(){return this.x;}},'b');
arr;//[1, 2, "a", Object, "b"]
arr.sort();//[1, 2, Object, "a", "b"]
arr.push(3);
arr.sort();//[1, 2, Object, 3, "a", "b"]  object在2和3之间,因为与比较时会调用toString()方法返回2
arr.reverse();//["b", "a", 3, Object, 2, 1]
//sort实现反序
arr.sort(function(va1,va2){if(va1<va2){return 1} else if(va1>va2){return -1}else{return 0}});//[1, 2, Object, 3, "a", "b"]
//concat合并数组,与String.prototype.concat作用相同
arr = [1,2,3];
arr.concat(['a','b']);//[1,2,3,'a','b'] 也可以arr.concat('a','b')
arr;//[1,2,3]不改变原数组
/**
*slice()它能够基于当前数组中的一或多个项创建一个新数组。 slice()方法可以
*接受一或两个参数,即要返回项的起始和结束位置。在只有一个参数的情况下, slice()方法返回从该
*参数指定位置开始到当前数组末尾的所有项。如果有两个参数,该方法返回起始和结束位置之间的项—
*—但不包括结束位置的项,不影响源数组
*也可以将类数组转换数组方法[].slice.call(类数组)
*常见的类数组有arguments,NodeList,styleSheets,HTMLCollection,HTMLFormControlsCollection
*HTMLOptionsCollection
*/
arr.slice(-1);//[3]
arr.slice(0,2);[1,2]
arr.push(4);
arr.slice(0,-1);//[1,2,3]
/**
*splice()功能强大
*1.传入两个参数,分别位置和要删除的项数。第二参数为负取0,不存在取数组长度
*2.还可传多个参数表示插入和替换
*/
arr = [1,2,3,4];
arr.splice(1);//[2,3,4]
arr = [1,2,3,4];
arr.splice(1,2);//[2,3]
arr = [1,2,3,4];
arr.splice(-1,2);//[4]
arr = [1,2,3,4];
arr.splice(-1,-2);//[]
arr.splice(1,0,9,9,9);//[1, 9, 9, 9, 2, 3, 4]在指定位置前插入
arr = [1,2,3,4];
arr.splice(1,2,9,9,9);//[1, 9, 9, 9, 4]替换

注明原文出处:https://www.cnblogs.com/jcheng996/p/5401088.html

猜你喜欢

转载自blog.csdn.net/gang456789/article/details/80845168