当前位置: 首页 > Web前端 > JavaScript

JS字符串的常用方法

时间:2023-03-27 17:49:18 JavaScript

Strings字符串的方法有很多,但是常用的只有那么几种,还有一些从来没有这样用过。下面简单介绍一下1.charAt(index):返回指定表下方的Value,返回一个新的字符串,不影响原来的字符串letstr='hello';letnewStr=str.charAt(1);console.log(str)//helloconsole.log(newStr)//e2.charCodeAt(index):返回下表中指定值的unicode值。返回值为Unicode编码,不影响原始字符串。letstr='hello';letnewStr=str.charCodeAt(1);console.log(str)//helloconsole.log(newStr)//1013.fromCharcode(u1,u2,u3):这是使用的静态方法通过String本身,参数是unicode编码,返回对应的值letstr=String.fromCharCode(72,69,76,76,79);letstr1=String.fromCharCode(97,98,99)console.log(str)//HELLOconsole.log(str1)//abc4.concat(val1,val2,val3,val4...):连接两个或多个字符串而不改变现有字符串,返回值是连接后的字符串letstr='hello';letnewStr=str.concat('world','!','.....');console.log(str)//helloconsole.log(newStr)//helloworld!.....5.indexOf(str,[start]):查找包含Value的字符串时,如果找到,返回第一个出现位置的索引值,如果没有找到,返回-1,start为查找的位置starts,start的位置包含在搜索范围内,可选,可以留空,默认0到开始检查。letstr='hello';console.log(str.indexOf('el'))//1console.log(str.indexOf('al'))//-1console.log(str.indexOf('el',2))//-16.lastIndexOf(str,[start]):lastIndexOf的使用方法与indexOf相同,只是返回最后一次出现的索引值,找不到则返回-1,而start是搜索的起始位置。它是可选的,您可以将其留空。默认是从string.length-1开始检查,也就是说从后往前检查。letstr='hello';console.log(str.lastIndexOf('l'))//3console.log(str.lastIndexOf('c'))//-1console.log(str.lastIndexOf('l',2))//27.replace(regexp|str,replacetext):用于将字符串中的某些字符串替换为其他字符串,但只能替换一次,也就是说,如果有多个字符串,但只有第一个匹配的值会被更换。另一种方式是使用正则,就是把符合正则的内容替换成你要替换的内容。原始字符串不会更改。letstr='helloworld';letnewStr1=str.replace('l','a')letnewStr2=str.replace(/\s*/g,'')letnewStr3=str.replace(/^\s*|\s*$/g,'')console.log(str)//'helloworld'console.log(newStr1)//'healoworld'console.log(newStr2)//'helloworld',这个就是去掉所有空格console.log(newStr3)//'helloworld',这个是去掉字符串两端的空格8.trim():删除字符串两端的空格。letstr='helloworld';letnewStr1=str.trim()console.log(str)//'helloworld'console.log(newStr1)//'healoworld'9.slice(start,[end]):截取字符串,在不改变原字符串的情况下,返回一个新的字符串。新的字符串从起始位置开始到结尾结束,但不包括结束位置letstr='helloworld';letnewStr1=str.slice(1)letnewStr2=str.slice(1,3)console.log(str)//'helloworld'console.log(newStr1)//'elloworld'console.log(newStr2)//'el'10.substr(start,[length]):截取字符串并返回一个新的字符串不会改变原来的字符串。新字符串从起始位置开始截取字符串的长度。如果没有,默认拦截到最后letstr='helloworld';letnewStr1=str.substr(1)letnewStr2=str.substr(1,3)console.log(str)//'helloworld'console.log(newStr1)//'elloworld'console.log(newStr2)//'ell'11.substring(start,[end]):截取字符字符串,不改变原字符串返回新字符串。新的字符串从开始位置开始到结尾结束,但不包括结束位置letstr='helloworld';letnewStr1=str.substring(1)letnewStr2=str.substring(1,3)console.log(str)//'helloworld'console.log(newStr1)//'elloworld'console.log(newStr2)//'el'12.split(separator,[limit]):将字符串拆分为An数组,第一个参数是指定的字符串或正则表达式,第二个参数是数组的最大长度,可选。letstr='helloworld';letstr1='hello,world';letnewStr1=str.split('')letnewStr2=str1.split(',')console.log(str)//helloworldconsole.log(newStr1)//['h','e','l','l','o','w','o','r','l','d']console.log(newStr2)//['hello','world']PS:字符串可以转数组,数组也可以转字符串,方法是join()letarr=['h','e','l','l','o','w','o','r','l','d']让str=arr.join("")console.log(str)13。toLowerCase(str):把字符字符串全部小写,英文letstr='HELLOWORLD';letnewStr1=str.toLowerCase('')console.log(str)//HELLOWORLDconsole.log(newStr1)//helloworld14.toUpperCase(str):String全部变成大写,英文letstr='helloworld';letnewStr1=str.toUpperCase('')console.log(str)//helloworldconsole.log(newStr1)//HELLOWORLD15.repeat(number):重复string,number必须是正整数letstr='helloworld';letnewStr1=str.repeat(3)console.log(str)//helloworldconsole.log(newStr1)//helloworldhelloworldhelloworld16.endsWith(str):检查字符串是否让str='helloworld';let以指定字符串结尾newStr1=str.endsWith('ld')console.log(str)//helloworldconsole.log(newStr1)//true17.includes(str):检查字符串是否包含指定的字符串或字符letstr='helloworld';letnewStr1=str.includes('ld')console.log(str)//helloworldconsole.log(newStr1)//true18.match(regexp):根据正则表达式或字符串查找字符串是否包含匹配项,返回数组ornullletstr='helloworld';letnewStr1=str.match('ld')console.log(str)//helloworldconsole.log(newStr1)//['ld',index:8,input:'helloworldld',groups:undefined]19.search(regexp):根据正则或字符串查找是否包含匹配项,有则返回索引,没有则返回-1。letstr='helloworld';letnewStr1=str.search('ld')console.log(str)//helloworldconsole.log(newStr1)//820.localeCompare(str):判断每个字母的排序,返回一个当前字符串前为正值,后为负值,同样只是return0letstr='helloworld';letnewStr1=str.localeCompare('ad')letnewStr2=str.localeCompare('helloworld')letnewStr3=str.localeCompare('helloworld')letnewStr4=str.localeCompare('id')console.log(str)//helloworldconsole.log(newStr1)//1console.log(newStr2)//0console.log(newStr3)//-1console.log(newStr4)//-1