javascript-String

267 阅读1分钟

这篇文章用于记录javascript String的属性及方法

属性

属性名 说明
constructor 构造函数
length 字符串的长度
_ proto_
String.prototype
原型对象,可向对象添加属性或方法

方法

ES5

方法名 说明
charAt(index) 返回指定位置的字符
charCodeAt(index) 返回指定位置字符的unicode编码
concat(elements) 链接两个或多个字符串,返回新的字符串
indexOf(str)
lastIndexOf(str)
返回指定字符在字符串首次出现的位置
match(regexp) 查找到正则表达式的匹配
search(regexp) 返回符合正则表达式的位置
slice(start, end) 截取字符串的片段并返回,左闭右开
replace(searchValue, replaceValue) 在字符串中查找匹配的字符串,替换为新的字符串并返回
split(separator) 将字符串按传入字符分割成字符串数组
substr(start, length) 返回从起始位置开始指定数目的字符
substring(start, end) 截取字符串的片段并返回,左闭右开
toLowerCase()
toUpperCase()
toLocalLowerCase()
toLocalUpperCase()
将字符串转换为小/大写
trim()
trimLeft()
trimRight
去掉字符串两边的空白
valueOf()
toString()
返回字符串原始值
localCompare(str) 用特定的顺序来比较两个字符串
String.fromCharCode(codes)
String.fromCodePonit(codes)
将unicode编码转为字符

ES6

方法名 说明
endsWith(searchSring, postion)
startsWith(searchSring, postion)
判断字符串是否以给定字符串结尾
includes(searchSring, postion) 判断字符串是否包含另一个字符串
repeat(count) 返回新的字符串,包含链接在一起指定数量的副本
String.raw() 用来处理模板字符串
normalize(form) 将字符串正规化

ES8

方法名 说明
padEnd(targetLength, padString)
padStart(targetLength, padString)
将字符串填充到指定长度