字符串方法

99 阅读1分钟

length

属性返回字符串的长度

indexOf()

方法返回字符串中指定文本首次出现的索引(位置)

lastIndexOf()

方法返回指定文本在字符串中最后一次出现的索引

search()

方法搜索特定值的字符串,并返回匹配的位置

提取部分字符串

slice(start, end)

substring(start, end)

substr(start, length)

slice()

提取字符串的某个部分并在新字符串中返回被提取的部分

substring()

类似于
slice()

不同之处在于
substring()
无法接受负的索引。

substr()

类似于
slice()

不同之处在于第二个参数规定被提取部分的长度。

replace()

方法用另一个值替换在字符串中指定的值

toUpperCase()

把字符串转换为大写

toLowerCase()

把字符串转换为小写

concat()

连接两个或多个字符串

trim()

方法删除字符串两端的空白符

提取字符串字符

charAt(position)

charCodeAt(position)

charAt()

方法返回字符串中指定下标(位置)的字符串

charCodeAt()

方法返回字符串中指定索引的字符
unicode
编码

split()

将字符串转换为数组