String常用的方法有十几种,经常把截取字符串的方法记混,又或者在开发过程中不能把这些方法用到恰到好处,还是再罗列一下这些方法以及用途,加深记忆,也便于查询
先后顺序无分重要性,下面的排版顺序是鉴于工作中使用的频率,也方便我个人记忆。
注意:字符串的这些方法都会返回一个新的变量,不会改变原本的字符串
(1)substring(indexStart, indexEnd)
(2)slice(beginIndex, endIndex)
两者的区别是,slice可以传入负数
(3)split(separator, limit)
字符串转化为数组
let arr = "a,b,c" .split(",")
arr[0] //a are[1] //b
- charAt(index)
- concat(string1, string2, ...)
- endsWith(searchString, position)
- includes(searchString, position)
- indexOf(searchValue, fromIndex)
- lastIndexOf(searchValue, fromIndex)
- localeCompare(compareString, locales, options)
- match(regexp)
- matchAll(regexp)
- normalize(form)
- padEnd(targetLength, padString)
- padStart(targetLength, padString)
- repeat(count)
- replace(searchValue, replaceValue)
- search(regexp)
- split(separator, limit)
- startsWith(searchString, position)
- toLocaleUpperCase(locales)
- toLowerCase()
- toString()
- toUpperCase()
- trim()
- trimEnd()
- trimStart()
- valueOf()