zoukankan      html  css  js  c++  java
  • js字符串方法汇总

    1、length方法

        var stringObject=new String("hellow world");
        console.log(stringObject.length);//12

    2、字符方法charAt()、charCodeAt()     指定索引查找字符

    这两个方法都接收一个参数

    charAt():返回给定位置的那个字符

    charCodeAt():返回指定位置的字符编码

        var stringValue="hellow world";
        console.log(stringValue.charAt(1));//e
        console.log(stringValue.charCodeAt(1));//101

    3、字符串操作方法concat()、slice()、substr()、substring()

    concat():用于将一个或多个字符串拼接起来,返回拼接得到的新字符串

    
    
    var stringValue="hellow world";
    var result=stringValue.concat("hello");
    var result2="hellow world"+"hello";
    console.log(stringValue);//hellow world
    console.log(result);//hellow worldhello
    console.log(result2);//hellow worldhello
    
    

    slice()、substr()、substring()都是截取字符串的方法,返回一个子字符串

    这三个方法都接收一个或两个参数,第一个参数指定子字符串的开始位置

    slice()和substring()方法的第二个参数是指定子字符串的结束位置,而substr()的第二个参数指定的则是返回的字符个数,这三个方法都不会修改字符串本身,都是返回一个子字符串

        var stringValue="hellow world";
        console.log(stringValue.slice(3));//low world
        console.log(stringValue.substring(3));//low world
        console.log(stringValue.substr(3));//low world
        console.log(stringValue.slice(3,7));//low 
        console.log(stringValue.substring(3,7));//low 
        console.log(stringValue.substr(3,7));//low wor
    
        //参数为负数的情况    都可以与length相加 继续执行
        //substring()会把负值 转换为0
        //slice()就是截取两个索引之间的字符
        //substr() 第二个参数为负值时,就是返回0个字符串 所以返回空字符串
        console.log(stringValue.slice(-3));//rld
        console.log(stringValue.substring(-3));//hellow world
        console.log(stringValue.substr(-3));//rld
        console.log(stringValue.slice(3,-4));//low w
        console.log(stringValue.substring(3,-4));//hel
        console.log(stringValue.substr(3,-4));//""

    4、字符串位置方法indexOf()、lastIndexOf()

    都接收一个或两个参数,第一个参数是要查的字符串,第二个参数是开始搜索的索引,返回匹配到字符串的索引,不存在则返回-1

    indexOf():是从前往后搜索

    lastIndexOf():是从后往前搜索

        var stringValue="hellow world";
        console.log(stringValue.indexOf("o"));//4
        console.log(stringValue.lastIndexOf("o"));//8
        console.log(stringValue.indexOf("o",6));//8
        console.log(stringValue.lastIndexOf("o",6));//4

    5、trim() 删除前后空格

        var stringValue="   hellow world   ";
        console.log(stringValue.trim());//"hellow world"

    6、大小写转换 toLowerCase()、toUpperCase()

    toLowerCase():将字符串每一项转换成小写

    toUpperCase():将字符串每一项转换成大写

        var stringValue="hellow WORLD";
        console.log(stringValue.toLowerCase());//hellow world
        console.log(stringValue.toUpperCase());//HELLOW WORLD

    7、match()、search()方法 匹配方法  过多介绍

    8、split()  将字符串转化为字符串数组

    接收一个或两个参数,第一个参数为分隔符,按照这分隔符分隔字符串。第二个参数指定返回字符串数组的长度

        var colors="red,blue,green,yellow"
        console.log(colors.split());//["red,blue,green,yellow"]
        console.log(colors.split(","));//["red", "blue", "green", "yellow"]
        console.log(colors.split(""));//["r", "e", "d", ",", "b", "l", "u", "e", ",", "g", "r", "e", "e", "n", ",", "y", "e", "l", "l", "o", "w"]
        console.log(colors.split(",",2));//["red", "blue"]

    9、string构造函数本身有一个fromCharCode()

    将多个字符编码转换为字符串

    console.log(String.fromCharCode(104,101,108,108,111));//hello

     10、replace()   替换方法

  • 相关阅读:
    python入门-函数(二)
    python入门-函数(一)
    python入门-WHILE循环
    python入门-用户输入
    python入门-字典
    Spring Security授权 AccessDecisionManager
    Java的性能优化
    datahub
    vbs mytest
    spring发布和接收定制的事件(spring事件传播)
  • 原文地址:https://www.cnblogs.com/fqh123/p/10367429.html
Copyright © 2011-2022 走看看