zoukankan      html  css  js  c++  java
  • 字符串常见方法

    字符串常见方法

    • indexOf( data, start );
        用于返回某个数组或者字符串中规定字符或者字符串的位置;
    var str = "hello world";
    console.log(str.indexOf("o"));          // 4
    console.log(str.indexOf("o",5));        // 7
    console.log(str.indexOf("o",8));        // -1
    • chartAt( index )
        返回指定位置的字符,index为下标
    var str = "hello world";
    console.log(str.charAt(3));             // l
    console.log(str.charAt(6));             // w
    • slice(m,n)
        根据指定位置,截取子串,从m到n,不包括n
    var str = "hello world";
    console.log(str.slice(2,7));            // "llo w"
    • substr(m,n)
        根据指定位置,截取子串,从索引m位置开始取,取n个
    var str = "hello world";
    console.log(str.substr(2,7));           // "llo wor"
    • substring(m,n):同slice(m,n)一样
        根据指定位置,截取子串,从m到n,不包括n
        如果不指定结束位置,则从开始位置到结尾
    var str = "hello world";
    console.log(str.substring(2,7));        // "llo w"
    • replace( oldstr, newstr )
        替换,每次只能替换一个符合条件的字符
    var str = "hello world";
    console.log(str.replace("o","哦"));     // hell哦 world 
    console.log(str.replace("o","哦"));     // hell哦 world
    
    "因为.replace()没有修改原字符;想要多次替换,如下:"
    var a = str.replace("o","哦");        
    console.log(a)                          // hell哦 world
    console.log(a.replace("o","哦"))        // hell哦 w哦rld
    • split( ):将字符转成字符
        通过指定字符分割字符串,返回一个数组
    var str = "hello world";
    console.log(str.split())                // Array [ "hello world" ]
    console.log(str.split(""))              // Array(11) [ "h", "e", "l", "l", "o", " ", "w", "o", "r", "l", … ]
    console.log(str.split("o"))             // Array(3) [ "hell", " w", "rld" ]
  • 相关阅读:
    关于有序查找的随笔
    Spring框架(一) 创建bean
    Linux常用命令
    Spring框架(二) bean的歧义性
    java实现图片文字识别的两种方法
    分享基于分布式Http长连接框架代码模型
    分享基于分布式Http长连接框架设计模型
    无限树Jquery插件zTree的使用方法
    分享基于分布式Http长连接框架
    使用vs编译事件来动态发布配置文件
  • 原文地址:https://www.cnblogs.com/wufenfen/p/11772045.html
Copyright © 2011-2022 走看看