ES5
方法 | 描述 | 详细内容 |
---|---|---|
charAt() | 返回指定索引位置的字符 | string.charAt(index) |
charCodeAt() | 返回指定索引位置字符的 Unicode 值 | string.charCodeAt(index) |
concat() | 连接两个或多个字符串,返回连接后的字符串 | string.concat(stringX,stringX,…,stringX) |
fromCharCode() | 将 Unicode 转换为字符串 | String.fromCharCode(n1, n2, …, nX) |
indexOf() | 返回字符串中检索指定字符第一次出现的位置 | string.indexOf(searchvalue,fromindex) |
lastIndexOf() | 返回字符串中检索指定字符最后一次出现的位置 | stringObject.lastIndexOf(searchvalue,fromindex) |
localeCompare() | 用本地特定的顺序来比较两个字符串 | stringObject.localeCompare(target) |
match() | 找到一个或多个正则表达式的匹配 | stringObject.match(searchvalue) stringObject.match(regexp) |
replace() | 替换与正则表达式匹配的子串 | stringObject.replace(regexp/substr,replacement) |
search() | 检索与正则表达式相匹配的值 | stringObject.search(regexp) |
slice() | 提取字符串的片断,并在新的字符串中返回被提取的部分 | arrayObject.slice(start,end) |
split() | 把字符串分割为子字符串数组 | stringObject.split(separator,howmany) |
substr() | 从起始索引号提取字符串中指定数目的字符 | stringObject.substr(start,length) |
substring() | 提取字符串中两个指定的索引号之间的字符 | stringObject.substring(start,stop) |
toLocaleLowerCase() | 根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射 | stringObject.toLocaleLowerCase() |
toLocaleUpperCase() | 根据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射 | stringObject.toLocaleUpperCase() |
toLowerCase() | 把字符串转换为小写 | stringObject.toLowerCase() |
toString() | 返回字符串对象值 | NumberObject.toString(radix) |
toUpperCase() | 把字符串转换为大写 | stringObject.toUpperCase() |
trim() | 移除字符串首尾空白 | string.trim() |
valueOf() | 返回某个字符串对象的原始值 | mathObject.valueOf() |
例子:
(1)
let x="abcde";
console.log(x.charAt(0)); // a
console.log(x.charCodeAt(0)); // 97
(2)
let x="abcde";
let y="xx";
console.log(x.concat(y)) // abcedxx
(3)
console.log(String.fromCharCode(66)) // B
(4)
let x="abcde";
console.log(x.indexOf("c")) // 2
console.log(x.lastIndexOf("d")) // 3
(5)
let x="老虎";
let y="大象";
console.log(x.charCodeAt(0))
console.log(y.charCodeAt(0))
console.log(x.localeCompare(y)) // 1
这里需要说明一下,localeCompare主要是用于比较本地的规则,那么本地的规则是什么呢?简单的讲,就是
比较两个大小,比如,x是老虎,y是大象,转换为特定编码后,x要比y大,所以,最后返回就是1,如果相等,就是返回0,如果是小于,就是返回-1;
(6)
(7)
let x="abcedfa";
console.log(x.slice(2,4)) //ce
(8)
let x="abcedfa";
console.log(x.split("",4)) //["a","b","c","e"]
(9)
let x="abcedfa";
console.log(x.substr(3,2)) //ed
console.log(x.substring(3,5)) //ed
(10)
let x="abcedfa";
console.log(x.toLocaleLowerCase()) //abcedfa
console.log(x.toLocaleUpperCase()) //ABCEDFA
console.log(x.toLowerCase()) //abcedfa
console.log(x.toUpperCase()) //ABCEDFA
ES6
方法 | 描述内容 | 详细 |
---|---|---|
includes() | 返回布尔值,判断是否找到参数字符串 | arr.includes(searchElement) arr.includes(searchElement, fromIndex) |
startsWith() | 返回布尔值,判断参数字符串是否在原字符串的头部 | string.startsWith(searchvalue, start) |
endsWith() | 返回布尔值,判断参数字符串是否在原字符串的尾部 | string.endsWith(searchvalue, start) |
repeat() | 方法字符串复制指定次数。 | string.repeat(count) |
padStart() | 返回新的字符串,表示用参数字符串从头部(左侧)补全原字符串。 | padStart(targetLength) padStart(targetLength, padString) |
padEnd() | 返回新的字符串,表示用参数字符串从尾部(右侧)补全原字符串。 | str.padEnd(targetLength [, padString]) |
例子
let x="bb abcedf a cc";
console.log(x.includes("c")) //true
console.log(x.startsWith("a")) //false
console.log(x.endsWith("c")) //true
console.log(x.repeat(2)) //bb abcedf a ccbb abcedf a cc
console.log(x.padStart(20,"22")) //222222bb abcedf a cc
console.log(x.padEnd(20,"22")) //bb abcedf a cc222222