js常用的字符串方法
- ES5
- concat(string…)连接多个字符串不改变原数组
var str = 'a' console.log(str.concat('b','c')) //'abc'
- replace(regexp/substr, replacement)
- 字符串替换
如果第一个参数传入的是子字符串或是没有进行全局匹配的正则表达式,那么replace()方法将只进行一次替换(替换最前面的),返回经过一次替换后的结果字符串不改变原数组
var str = 'abcabcabcd' console.log(str.replace('a','A')) //Abcabcabcd console.log(str.replace(/a/,'A')) //Abcabcabcd
如果第一个参数传入的全局匹配(/g)的正则表达式,那么replace()将会对符合条件的子字符串进行多次替换,最后返回经过多次替换的结果字符串
var str = 'abcabcabcd' console.log(str.replace(/a/g,'A')) //AbcAbcAbcd
- substring(start, end)
- 返回一个新的字符串,其内容是从start处到end-1处的所有字符。若结束参数(end)省略,则表示从start位置一直截取到最后,传入负值则视为0不改变原数组
var str = 'abcdefg' console.log(str.substring(2)) //cdefg console.log(str.substring(3,5)) //de console.log(str.substring(-2)) //abcdefg
- slice(start, end)
分割字符串slice()方法与substring()方法非常类似,它传入的两个参数也分别对应着开始位置和结束位置。而区别在于,slice()中的参数可以为负值,如果参数是负数,则该参数规定的是从字符串的尾部开始算起的位置。也就是说,-1 指字符串的最后一个字符。不改变原数组var str = 'abcdefg' console.log(str.slice(2)) //cdefg console.log(str.slice(3,5)) //de console.log(str.slice(-2)) //fg console.log(str.slice(-2,-4)) //负数不能这样写 console.log(str.slice(-4,-2)) //de console.log(str.slice(2,-2)) //cde
- substr(start, length)
- 在字符串中抽取从start下标开始的指定数目的字符。其返回值为一个字符串,包含从 stringObject的start(包括start所指的字符)处开始的length个字符。如果没有指定 length,那么返回的字符串包含从start到stringObject的结尾的字符。另外如果start为负数,则表示从字符串尾部开始算起。
var str = 'abcdefg' console.log(str.substr(2)) //cdefg console.log(str.substr(2,3)) //cde console.log(str.substr(-2,5)) //fg
- charAt(index)
- 获取指定位置的字符串,若不在这个范围将返回一个空字符串
var str = 'abcdefg' console.log(str.charAt(2)) //'c' console.log(str.charAt(10)) //''
- charCodeAt(index)
- 返回指定位置的字符的Unicode编码
var str = 'abcdefg' console.log(str.charCodeAt(2)) //99 console.log(str.charCodeAt(10)) //NAN
- indexOf(searchvalue, fromindex)
- 检索指定的字符串值在字符串中首次出现的位置
- 接收两个参数:searchvalue表示要查找的子字符串,fromindex表示查找的开始位置,省略的话则从开始位置进行检索。
var str = 'abcabcabc' console.log(str.indexOf('b')) //1 console.log(str.indexOf('a',5)) //6 console.log(str.indexOf('ab',1)) //1 console.log(str.indexOf('ac',1)) //-1
- lastIndexOf(searchvalue, fromindex)
- 返回的是一个指定的子字符串值最后出现的位置,其检索顺序是从后向前
var str = 'abcabcabc' console.log(str.lastIndexOf('b')) //7 console.log(str.lastIndexOf('a',5)) //3 console.log(str.lastIndexOf('bc')) //7 console.log(str.lastIndexOf('ac')) //-1
- match(substr / regexp)
- 在字符串内检索指定的值,或找到一个或多个正则表达式的匹配
- 如果参数中传入的是子字符串或是没有进行全局匹配的正则表达式,那么match()方法会从开始位置执行一次匹配,如果没有匹配到结果,则返回null。否则则会返回一个数组,该数组的第0个元素存放的是匹配文本,除此之外,返回的数组还含有两个对象属性index和input,分别表示匹配文本的起始字符索引和stringObject 的引用(即原字符串)。
var str = '1a2b3c4d5e' console.log(str.match('3c')) //["3c", index: 4, input: "1a2b3c4d5e", groups: undefined] console.log(str.match('h')) //null console.log(str.match(/b/)) //["b", index: 3, input: "1a2b3c4d5e", groups: undefined] //如果参数传入的是具有全局匹配(g标识)的正则表达式,那么match()从开始位置进行多次匹配,直到最后。 //如果没有匹配到结果,则返回null。否则则会返回一个数组,数组中存放所有符合要求的子字符串,并且没有index和input属性。 console.log(str.match(/\d/g)) //["1", "2", "3", "4", "5"] console.log(str.match(/d/g)) // ["d"] console.log('abcddddd'.match(/d/g)) //["d", "d", "d", "d", "d"]
- split(separator, howmany)
字符串转数组- 第一个参数separator表示分割位置(参考符),第二个参数howmany表示返回数组的允许最大长度(一般情况下不设置)
var str = 'abcdefg' console.log(str.split()) //["abcdefg"] console.log(str.split('')) //["a", "b", "c", "d", "e", "f", "g"] console.log(str.split(' ')) //["abcdefg"] var str1 = 'a|b|c|d|e|f|g' console.log(str1.split('|')) //["a", "b", "c", "d", "e", "f", "g"] console.log(str1.split('')) //["a", "|", "b", "|", "c", "|", "d", "|", "e", "|", "f", "|", "g"]
- toLowerCase()
- 把字符串中的大写字母转换为小写
var str = 'abcDEF' console.log(str.toLowerCase()) // abcdef
- toUpperCase()
- 把字符串中的小写字母转换为大写
var str = 'abcDEF' console.log(str.toUpperCase()) // ABCDEF
- ES6
- includes(searchvalue, start)
- 判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。
var str = "Hello world, welcome to the Runoob"; console.log(str.includes("world")) //true console.log(str.includes("world",12)) //false
- startsWith(searchvalue, start)
- 判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true 或 false。
var str = "Hello world, welcome to the Runoob"; console.log(str.startsWith("Hello")) //true console.log(str.startsWith("world")) //false console.log(str.startsWith("world",6)) //true
- endsWith(searchvalue, length)
- 判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。
var str = "Hello world, welcome to the Runoob"; console.log(str.endsWith("Runoob")) //true console.log(str.endsWith("world")) //false console.log(str.endsWith("world",11)) //true
- repeat(count)
- 字符串复制指定次数
var str = "Runoob"; console.log(str.repeat(2)) //RunoobRunoob
- concat(string…)连接多个字符串不改变原数组