返回

JS 字符串方法巧用,编程世界大不同

前端

JS 中的字符串方法

  1. charAt(x)

    • charAt(x)返回字符串中x位置的字符,下标从 0 开始。
  2. charCodeAt(x)

    • charCodeAt(x)返回字符串中x位置处字符的unicode值。
  3. concat()

    • concat() 方法用于连接两个或多个字符串,返回连接后的字符串。
  4. indexOf(substring, start)

    • indexOf(substring, start)方法用于查找字符串中从start位置开始第一次出现substring的位置,如果找不到,则返回-1。
  5. lastIndexOf(substring, start)

    • lastIndexOf(substring, start)方法用于查找字符串中从start位置开始最后一次出现substring的位置,如果找不到,则返回-1。
  6. match(regexp)

    • match(regexp)方法用于查找字符串中与正则表达式regexp匹配的所有子字符串,并返回一个数组。
  7. replace(regexp, newstr)

    • replace(regexp, newstr)方法用于用newstr替换字符串中与正则表达式regexp匹配的所有子字符串,并返回替换后的字符串。
  8. search(regexp)

    • search(regexp)方法用于查找字符串中与正则表达式regexp匹配的第一个子字符串的位置,如果找不到,则返回-1。
  9. slice(start, end)

    • slice(start, end)方法用于从字符串中提取从start位置到end位置(不包括end位置)的子字符串,并返回提取出的子字符串。
  10. split(separator, limit)

    • split(separator, limit)方法用于将字符串按照separator分隔符分隔成一个数组,如果limit参数存在,则只分隔limit个子字符串。
  11. substr(start, length)

    • substr(start, length)方法用于从字符串中提取从start位置开始的length长度的子字符串,并返回提取出的子字符串。
  12. substring(start, end)

    • substring(start, end)方法用于从字符串中提取从start位置到end位置(不包括end位置)的子字符串,并返回提取出的子字符串。
  13. toLocaleLowerCase()

    • toLocaleLowerCase()方法用于将字符串转换为小写,并返回转换后的字符串。
  14. toLocaleUpperCase()

    • toLocaleUpperCase()方法用于将字符串转换为大写,并返回转换后的字符串。
  15. toLowerCase()

    • toLowerCase()方法用于将字符串转换为小写,并返回转换后的字符串。
  16. toUpperCase()

    • toUpperCase()方法用于将字符串转换为大写,并返回转换后的字符串。
  17. trim()

    • trim()方法用于去除字符串两端的空白字符,并返回去除后的字符串。
  18. valueOf()

    • valueOf()方法用于返回字符串的原始值。
  19. toString()

    • toString()方法用于将字符串转换为字符串对象,并返回字符串对象。
  20. length

    • length属性用于返回字符串的长度。