返回
JS 字符串方法巧用,编程世界大不同
前端
2023-09-23 14:36:52
JS 中的字符串方法
-
charAt(x)
- charAt(x)返回字符串中x位置的字符,下标从 0 开始。
-
charCodeAt(x)
- charCodeAt(x)返回字符串中x位置处字符的unicode值。
-
concat()
- concat() 方法用于连接两个或多个字符串,返回连接后的字符串。
-
indexOf(substring, start)
- indexOf(substring, start)方法用于查找字符串中从start位置开始第一次出现substring的位置,如果找不到,则返回-1。
-
lastIndexOf(substring, start)
- lastIndexOf(substring, start)方法用于查找字符串中从start位置开始最后一次出现substring的位置,如果找不到,则返回-1。
-
match(regexp)
- match(regexp)方法用于查找字符串中与正则表达式regexp匹配的所有子字符串,并返回一个数组。
-
replace(regexp, newstr)
- replace(regexp, newstr)方法用于用newstr替换字符串中与正则表达式regexp匹配的所有子字符串,并返回替换后的字符串。
-
search(regexp)
- search(regexp)方法用于查找字符串中与正则表达式regexp匹配的第一个子字符串的位置,如果找不到,则返回-1。
-
slice(start, end)
- slice(start, end)方法用于从字符串中提取从start位置到end位置(不包括end位置)的子字符串,并返回提取出的子字符串。
-
split(separator, limit)
- split(separator, limit)方法用于将字符串按照separator分隔符分隔成一个数组,如果limit参数存在,则只分隔limit个子字符串。
-
substr(start, length)
- substr(start, length)方法用于从字符串中提取从start位置开始的length长度的子字符串,并返回提取出的子字符串。
-
substring(start, end)
- substring(start, end)方法用于从字符串中提取从start位置到end位置(不包括end位置)的子字符串,并返回提取出的子字符串。
-
toLocaleLowerCase()
- toLocaleLowerCase()方法用于将字符串转换为小写,并返回转换后的字符串。
-
toLocaleUpperCase()
- toLocaleUpperCase()方法用于将字符串转换为大写,并返回转换后的字符串。
-
toLowerCase()
- toLowerCase()方法用于将字符串转换为小写,并返回转换后的字符串。
-
toUpperCase()
- toUpperCase()方法用于将字符串转换为大写,并返回转换后的字符串。
-
trim()
- trim()方法用于去除字符串两端的空白字符,并返回去除后的字符串。
-
valueOf()
- valueOf()方法用于返回字符串的原始值。
-
toString()
- toString()方法用于将字符串转换为字符串对象,并返回字符串对象。
-
length
- length属性用于返回字符串的长度。