js—字符串的方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        var str ="hellboqintian";
        /*
            在底层字符串是以字符数组的形式保存
            ["f","s","w","f"]
            length可以用来获取字符串的长度
        */
        document.write(str.length);
        /*
          charAt()
             可以返回字符串中指定位置的字符
             根据索引获取指定的字符
        */
        var result=str.charAt(5);
        document.write(result);
        /*
           charCodeAt()
             获取指定位置字符的字符编码(unicode编码)
        */
        var result=str.charCodeAt(0);
        document.write(result);
        /*
          String.fromCharcode()
            -可以根据字符编码去获取字符
        */
        document.write("<hr>")
        result=String.fromCharCode(0x2686);
        document.write(result);
        /*
           concat()
              -可以用来连接两个或多个字符串
              -作用和+一样
        */
        result=str.concat("你好","明天");
        document.write(result);
        /*
          indexof()
             -该方法可以检索字符串中是否含有指定内容
              如果字符串中含有改内容,则会返回第一次出现的索引
              如果没有找到指定的内容,则返回-1
           lastIndexOf();
            与indexof用法相同,不同的是lastindexof从后往前找,
            也可以指定开始查找参数
        */
        document.write("<hr>")
        str="hello atgui";
        result=str.indexOf("h",5);//5代表从第五个位置开始查找
        result1=str.indexOf("e");
        result2=str.indexOf("w");
        result4=str.lastIndexOf("g");
        document.write(result);
        document.write(result1);
        document.write(result2);
        document.write("<hr>");
        document.write(result4);
        /*
           slice()
               从字符串中截取指定的内容
               不会影响原字符串。而是将截取到的内容返回
               参数:
                  第一个是开始位置的索引,包括开始
                  第二个是结束位置的索引,不包括结束
                  如果省略第二个参数,则会截取第一个参数后边所有的字符串
                  也可以传递一个负数作为参数,代表倒数第几个
            substring()
                 可以用来截取一个字符串,和slice类似
                 参数:
                    第一个:开始截取位置的索引(包括开始位置)
                    第二个:结束位置的索引(不包括结束位置)
                    不同的是substring()不能接受负值作为参数
                    如果传递了一个负值,则默认使用0
                    而且会自动调整参数的位置
                    如果第二个参数小于第一个,则自动交换

        */
        document.write("<hr>");
        str="iouxdhbsiajo";
        result=str.slice(0,5);
        result=str.slice(3);//3以后全要
        document.write(result);
        /*
          substr():
             用来截取字符串
             参数:
                1.截取开始位置的索引
                2.截取的长度
        */
        document.write("<hr>");
        document.write("<hr>");
        str="audbndc";
        result=str.substr(1,6);
        document.write(result);
        /*
          split()
              可以将一个字符串拆分为一个数组
              需要一个字符作为参数,将会根据该字符串去拆分数组
              如果传递一个空串作为参数,则会将每个字符都拆分为数组中的一个元素

        */
        document.write("<hr>");
        str="abc,bfa,ea,fes";
        result=str.split("b");
        document.write(result);
        result=str.split("");//空串,拆分
        document.write(result);
        /*
          toLowerCase()  把字符串转化为小写
          toUpperCase()  把字符串转化为大写

        */
        document.write("<hr>");
        str="adghbwhYHGVTCjk";
        result=str.toLowerCase();
        document.write(result);
        result=str.toUpperCase();
        document.write(result);

    </script>
</head>
<body>
    
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_44158539/article/details/116262678