js 字符串操作

字符串操作也是项目中处理返回数据常用到的。

 

ES5:

split():字符串转数组,默认不隔开=>返回数组

substr(a,b):从a处开始,截取字符串度为b的字符串=>返回指定字符串

indexOf(a):检索字符串位置,没有返回-1=>返回指定字符串位置

replace(a,b):将a位置的字符替换成b=>返回替换后的字符串

parseInt():字符串转数字=>返回数字

match(reg):字符串过滤=>返回符合正则的字符串

toLowerCase():字符串转小写=>返回字符串

toUpperCase():字符串转大写=>返回字符串

trim():去掉空格=>返回字符串

charAt(index):返回指定位置的字符

substring(a,b):从a处开始,截取到b位置=>返回指定字符串

 

ES6:

includes(searchString,position):表示是否找到了指定的参数字符串,返回true or false

position:表示指定字符串开始位置

var str='abcdefg';
str.includes('a')   //true
str.includes('A')   //false
str.includes('a',0) //true
str.includes('a',3)   //false

startsWith(searchString,position)):表示是否以指定的字符串为开头,返回true or false

position:表示指定字符串开始位置

var str='abcdefg';
str.startsWith('a');   //true
str.startsWith('c');   //false
str.startsWith('ab',0); //true
str.startsWith('de',3);   //true

endsWith(searchString,position)):表示是否以指定的字符串为开头,返回true or false

position:表示指定字符串结束位置

var str='abcdefg';
str.endsWith('g');   //true
str.endsWith('c');   //false
str.endsWith('bc',3); //true
str.endsWith('bc',2);   //false

 

posted @ 2018-05-07 16:02  Artimis  阅读(183)  评论(0编辑  收藏  举报