zoukankan      html  css  js  c++  java
  • JavaScript字符串String方法介绍及使用示例

    实例方法

    charAt() charCodeAt() 返回索引位置的字符

    'hello'.charAt(0) //h  等价 'hello'.[0]
    
    //返回索引位置的字符的Unicode码点
    'hello'.charCodeAt(0) // 104
    

    slice() 截取字符串,返回截取的字符串

    'hello'.slice(1) //ello
    'hello'.slice(2,4) //ll ①参数:截取开始位置  ②参数:截取结尾
    'hello'.slice(-3,-1) //ll 负值,表示结尾往前数
    

    substring() substr() 截取字符串,但参数更加自由

    'hello'.substring(0,1) //h
    'hello'.substring(1,0) //h 二参数比一参数小时,会自动调换
    'hello'.substring(-2,1) //h 参数为负是自动变0
    'hello'.substring(4,-2) // (4,-2)-> (4,0) -> (0,4) hell
    

    concat() 合并字符串

    const str = 'hello'.concat('-','world') //hello-world
    const strN = ''.concat(1,2) // 12 对于非字符串会转为字符串
    

    split() 按指定规则切割字符串为数组

    'hello'.split() // ['hello']
    'hello'.split('') //  ["h","e","l","l","l","o"]
    'hello-world'.split('-') // ["hello","world"]
    

    indexOf() lastIndexOf() 获取字符在字符串中的索引

    'hello'.indexOf('h') //0
    

    replace() 替换字符串,参数可为正则表达式,不改变原字符串

    // 不使用正则只能替换一个
    'hello'.replace('h','f') // fello
    
    // 使用正则可替换多个,es6中引入了replaceAll()也可
    'helloh'.replace(/h/g,'f') // fellof
    

    match() 可以确定原字符串是否匹配子字符串,返回一数组,该数组含有index,input属性(常正则时使用)

    const arr = 'hell,hello,heal'.match('he')
    //arr 是 ["he"]
    //arr.index 是 1
    //arr.input 是 'hell,hello,heal'
    

    search() 与match()类似,但匹配成功返回第一匹配的索引,不成功返回-1

    'hell,hello,heal'.search('he') //0
    

    trim() 去除头部或者尾部空格

    ' he llo '.trim() //'he llo'
    

    toUpperCase() toLowerCase() 将字符串转换为大写 小写

    'hello'.toUpperCase() //HELLO
    

    ES6拓展内容

    1.添加了遍历器Iterator接口,可for...of

    2.模板字符串``

    var a = 2; var foo = () => {}
    `
    模板字符串
    可换行
    可引用变量${a} $(foo())
    大括号内容可任意表达式,可运算,可对象引用
    `
    `
    模板编译
    在<%= %> 放表达式
    <% %> 放JavaScript代码
    <ul>
    <% for(var i = 0; i < 10; i++){ %>
    <li><%= i + 1 %></li>
    <% } %>
    </ul>
    `
    

    标签模板:特殊的函数调用形式

    3.新增方法
    实例方法:
    includes() startsWith() endsWith() 返回布尔值,是否包含,是否匹配开始,是否匹配结尾

    const str = 'Hello world!';
    
    str.startsWith('Hello') // true
    str.endsWith('!') // true
    str.includes('o') // true
    

    repeat() 重复次数,复读机

    'hello'.repeat(3) //hellohellohello
    

    padStart() padEnd() 补全字符串位数,第一个参数是字符串补全生效的最大长度,第二个参数是用来补全的字符串

    '01'.padStart(4,'20') //2001 头部补全
    '23'.padEnd(3,'s') //23s 尾部补全
    

    trimStart() trimEnd() 分别去除头部,尾部空格

    const str = ' he llo '
    str.trim() //'he llo'
    str.trimStart() //'he llo '
    str.trimEnd() //' he llo'
    

    replaceAll() 替换字符串

    'helloh'.replaceAll('h','f') //fellof
    
    //使用正则时必须有g修饰符,否则报错
    'helloh'.replaceAll(/h/g,'f') //fellof
    

    matchAll()

  • 相关阅读:
    EntytyFramework批量更新
    EntityFramework批量Insert
    asp.net MVC Session锁的问题
    谈谈ThreadStatic
    C#中XmlSerializer的内存占用问题
    转:【译】Asp.net MVC 利用自定义RouteHandler来防止图片盗链
    面试心得
    Part 39 AngularJS route change events
    Part 38 AngularJS cancel route change
    Part 37 Difference between $scope and $rootScope
  • 原文地址:https://www.cnblogs.com/flytree/p/14659975.html
Copyright © 2011-2022 走看看