1.js中string常用方法总结
学习网址:MDN Web Docs
<script>
/*String常用的方法总结*/
//1.(在ECMAScript 5中有所介绍) 是把字符串当作一个类似数组的对象,不可以删除或添加
var str = 'hello';
console.log(str[0]); //h
//2.String.length:属性表示一个字符串的长度。
console.log(str.length) //5
//3.str.charAt(index):方法从一个字符串中返回指定的字符。
console.log(str.charAt(0)) //h
//4.str.charCodeAt(index):方法整数,表示给定索引处的Unicode码
console.log('abc'.charCodeAt(0)); //97
//5.str.concat(str2, [, ...strN]) :合并字符串
//方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。
var str = String.prototype.concat('hello', 'world');
var str1 = '';
console.log(str); //helloworld
console.log(str1.concat('hello', 'world')); //helloworld
//6.str.endsWith(searchString[, length]):检查字符串是否以某字符开头
//方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true
var str1 = 'hello world';
console.log(str1.endsWith('orld')) //true //这个方法是大小写敏感的。
console.log(str1.endsWith('World')) //false
//7.String.fromCharCode(num1[, ...[, numN]])
//返回由指定的unicode组成的字符串。
console.log(String.fromCharCode(97, 98, 99)); //abc
//8.str.includes(searchString[, position]):字符串是否包含某个字符字符串
//方法用于str中是否包含searchString字符串,根据情况返回 true 或 false。
//position:从当前字符串的哪个索引位置开始搜寻子字符串,默认值为 0
console.log('blue and yellow'.includes('blue')); //true
console.log('blue and yellow'.includes('Blue')); //false 大小写敏感
console.log('blue and yellow'.includes('blue', 1)) //false
//9.str.indexOf(searchValue [, fromIndex]):返回字符的索引
//方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。
console.log('hello world'.indexOf('o')); //4
console.log('hello world hello china'.indexOf('w')); //6
console.log('hello world hello china'.indexOf('world')) //6
//10.str.lastIndexOf(searchValue[, fromIndex]):从末尾查找某个字符的索引
//方法返回调用String 对象的指定值最后一次出现的索引,在一个字符串中的指定位置 fromIndex处从后向前搜索。如果没找到这个特定值则返回-1 。
console.log('ac bb aa cc'.lastIndexOf('c')) //10
//11.referenceStr.localeCompare(compareString[, locales[, options]]):比较字符unicode码的大小
//方法返回一个数字来指示一个参考字符串是否在排序顺序前面或之后或与给定字符串相
console.log('a'.localeCompare('b')); //-1
console.log('ca'.localeCompare('ab')) //1
//12.str.match(regexp):方法检索返回一个字符串匹配正则表达式的结果。
var reg = /[a-c]/g;
console.log('abc'.match(reg)); //['a', 'b', 'c']
console.log('abC'.match(reg)); // ['a', 'b']
//13.str.padEnd(targetLength [, padString]):末尾加字符
//方法会用一个字符串填充当前字符串(如果需要的话则重复填充),返回填充后达到指定长度的字符串。从当前字符串的末尾(右侧)开始填充。
//targetLength:当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身
var str = 'hello';
console.log(str.padEnd(10, 'worldo')) //helloworld
//14.str.padStart(targetLength [, padString]) :开头加字符
//方法用另一个字符串填充当前字符串(如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。
console.log('hello'.padStart(10, 'world')) //worldhello
console.log('hello'.padStart(5, 'world')); //hello
console.log('hello'.padStart(6, 'world')); //whello
//15.str.repeat(count):重复字符串
//构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。
console.log('hello'.repeat(2)); //hellohello
//16.str.replace(regexp|substr, newSubStr|function):替换字符串,只进行一次
//方法返回一个由替换值(replacement)替换部分或所有的模式(pattern)匹配项后的新字符串
console.log('hello world hello world'.replace('world', 'china')); //hello china hello world
//17.const newStr = str.replaceAll(regexp|substr, newSubstr|function):替换字符串,全部替换
//方法返回一个新字符串,新字符串所有满足 pattern 的部分都已被replacement 替换。
console.log('hello world hello world'.replaceAll('world', 'china')); //hello china hello china
//18.str.search(regexp):方法执行正则表达式和 String 对象之间的一个搜索匹配。
//如果匹配成功,则 search() 返回正则表达式在字符串中首次匹配项的索引;否则,返回 -1。
var reg = /[A-Z]/g;
console.log('abc ABC'.search(reg)); //4
//19.str.slice(beginIndex[, endIndex]):切去某段字符串
//方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。
console.log('hello world'.slice(6)); //world
//20.str.split([separator[, limit]]):字符串转化成数组
//方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置。
console.log('hello world'.split()); //['hello world']
console.log('hello world hello china'.split(' ')) // ['hello', 'world', 'hello', 'china']
//21.str.startsWith(searchString[, position]):查找字符串是否已某字符开头
//方法用来判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果返回 true 或 false。
//position 可选:在 str 中搜索 searchString 的开始位置,默认值为 0。
console.log('hello world'.startsWith('hel')); //true
console.log('hello world'.startsWith('hel', 5)); //false
//22.str.substring(indexStart[, indexEnd]):切割某段字符串
//方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集。
console.log('hello world'.substring(6)); //world
//23.str.toLocaleLowerCase(),有带参数的写法
//方法根据任何指定区域语言环境设置的大小写映射,返回调用字符串被转换为小写的格式。
console.log('hello WORLD'.toLocaleLowerCase()); //hello world
//24.str.toLocaleUpperCase():方法根据本地主机语言环境把字符串转换为大写格式,并返回转换后的字符串。
console.log('hello WORLD'.toLocaleUpperCase()); //HELLO WORLD
//25.str.toLowerCase():转换成小写
console.log('hello WORLD'.toLowerCase()); //hello world
//26.str.toUpperCase():转换成大写
console.log('hello WORLD'.toUpperCase()); //HELLO WORLD
//27.str.toString():转换成字符串
// 方法返回指定对象的字符串形式。
var num = 100;
console.log(typeof num.toString()); //string
//28.str.trim(): 方法会从一个字符串的两端删除空白字符。
console.log(' hello world '.trim()) //hello world
//29.str.trimEnd():方法从一个字符串的末端移除空白字符
console.log(' hello world '.trimEnd()) // hello world
//30.str.trimStart():方法从字符串的开头删除空格
console.log(' hello world '.trimStart()) //hello world
//31.str.valueOf():方法返回 String 对象的原始值
const stringObj = new String('foo');
console.log(stringObj); //String {'foo'}
console.log(stringObj.valueOf()); //foo
</script>