您的位置:首页 > Web前端 > JavaScript

js中字符串的方法总结

2021-08-05 01:49 1036 查看

js中字符串的方法总结

我们知道在JavaScript中,不仅引用类型也有方法和属性,而且string、number、boolean这三种基本数据类型也有方法和属性,有关详解可以参考我的上篇博文JavaScript中的包装类

  今天我们就来对字符串(严格地说,应该是 String对象)的属性和常用方法做个总结,有利于我们系统的学习和掌握

 

String对象的属性

  String对象一共有三个属性:

    constructor:对创建该对象的函数的引用

    length:返回字符串的长度

    prototype:允许您向对象添加属性和方法

  最常用的 length 属性

    实例:

    var str = 'beautiful'

    var len = str.length

    console.log(len)  // 9(字符串的长度为9)

 

String对象的方法

查找字符串中的字符串

  indexOf(string,strart)方法

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

    参数一:string 要查找的字符串

    参数二:start 查找的起始位置(可省略)

    实例:   

    var str = 'You are so beautiful and so cute'

    var pos = str.indexOf('so')

    console.log(pos)  // 8('so'首次出现在str中的索引位置为 8)

 

  lastIndexOf(string,start)方法

    - 返回字符串中指定文本最后一次出现的索引(位置)    

    参数一:string 要查找的字符串

    参数二:start 查找的起始位置(可省略) 

    实例:   

    var str = 'You are so beautiful and so cute'

    var pos = str.lastIndexOf('so')

    console.log(pos)  // 25('so'最后一次出现在str中的索引位置为 25)

  如果未找到文本,indexOf() 和 lastIndexOf()均返回 -1

 

  search(string)方法

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

    参数:string 要查找的字符串

    实例:   

    var str = 'You are so beautiful and so cute'

    var pos = str.search('so')

    console.log(pos)  // 8('so'首次出现在str中的索引位置为 8) 

其中,indexOf() 和 search()是相等的,

  不同的是:

    1.search()方法无法设置第二个开始位置参数

    2.indexOf()方法无法设置更强大的搜索值(正则表达式)

 

 

提取部分字符串

  有三种提取部分字符串的方法:

    1.slice(start,end)

    2.substring(start,end)

    3.substr(str,length)  

 

  slice(strart,end)方法

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

    参数一:start 起始索引(开始位置)

    参数二:end 结束索引(结束位置)可省略(省略则是裁剪开始位置到最后)

    实例:    

    var str = 'You are so beautiful'

    var res = str.slice(4,7)

    console.log(res)  // are

 

    如果某个参数为负,则从字符串的结尾开始计数

      下面例子裁剪字符串中位置 -17 到位置 -13 的片段   

    var str = 'You are so beautiful'

    var res = str.slice(-17,-13)

    console.log(res)  // are

 

  substring(strart,length)方法

    - 类似于 slice()

      不同的是 substring()无法接受负的索引

    如果省略第二个参数,则裁剪开始位置到字符串末尾

    实例:    

    var str = 'You are so beautiful'

    var res = str.substring(4,10)

    console.log(res)  // are so

 

  substr(strart,length)方法

    - 类似 slice(),不同的是第二个参数规定被提取部分的长度

    参数一:start 起始索引(开始位置)

    参数二:length 被提取部分的长度 可省略(省略将裁剪到字符串最后)

    同样:当 start 为负值时,从结尾开始计算

    实例:    

    var str = 'You are so beautiful'

    var res = str.substr(4,10)

    console.log(res)  // are so bea

 

 替换字符串内容

    replace(str,restr)方法      

    参数一:str 被替换的字符串

    参数二:restr 替换的字符串

      - 用另一个值替换在字符串中指定的值(且只会替换收个匹配)

      注意:该方法对大小写敏感,该方法可以匹配正则表达式!

    实例:    

    var str = 'You are so beautiful'

    var res = str.replace('beautiful','cute')

    console.log(res)  // You are so cute

 

转换为大写和小写

   toUpperCase()方法

    - 把字符串转换为大写

    注意:该方法对大小写敏感,该方法可以匹配正则表达式!!!

    实例:    

    var str = 'You are so beautiful'

    var res = str.toUpperCase()

    console.log(res)  // YOU ARE SO BEAUTIFUL

 

   toLowerCase()方法

    - 把字符串转换为小写

    注意:该方法对大小写敏感,该方法可以匹配正则表达式!!!

    实例:    

    var str = 'BEAUTIFUL'

    var res = str.toLowerCase()

    console.log(res)  // beautiful

 

concat()方法

  - 连接两个或多个字符串

    实例:    

    var str1 = 'hello'

    var str2 = 'world'

    var res = str1.concat(' ',str2)

    console.log(res)  // hello world

 

String.trim()方法

  - 删除字符串两端的空白符

    实例:    

    var str = '     hello world      '

    var res = str.trim()

    console.log(res)  // 'hello world'

 

提取字符串字符

  这是两个提取字符串字符的安全方法:

    1. charAt(position)

    2. charCodeAt(position) 

 

   charAt()方法

    - 返回字符串中指定下标(位置)的字符串:

    实例:    

    var str = 'You are so beautiful'

    var res = str.charAt(0)

    console.log(res)  // Y

 

   charCodeAt()方法

    - 返回字符串中指定索引的字符 unidoce 编码:

    实例:    

    var str = 'You are so beautiful'

    var res = str.charCodeAt(0)

    console.log(res)  // 89

 

属性访问

  ECMAScript 5(2009)允许对字符串的属性访问 [ ]:

    实例:    

    var str = 'You are so beautiful'

    console.log(str[0])  // Y

注意:

  1.属性访问是只读的,不能赋值

  2.找不到字符串,会返回 undefined,而 charAt()返回空字符串

  3.让字符串看起来像是数组(其实并不是)

 

把字符串转换为数组

   split(分隔符)方法

    - 将字符串转换为数组

    实例:    

    var str = 'You are so beautiful'

    var arr = str.split(' ')

    console.log(arr)  // ['You', 'are', 'so', 'beautiful']

 

支持正则表达式的 String 对象的方法

  search  检索与正则表达式相匹配的值

  match  找到一个或多个正则表达式的匹配

  replace  替换与正则表达式匹配的子串

  split  把字符串分割为字符串数组

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: