js_字符串对象

<!--@description-->
<!--@author beyondx-->
<!--@date Created in 2022/07/31/ 15:01-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>字符串对象</title>
</head>
<body>
    <script>
        // var s = 'kjawnz3bxrdzerxdbvf';
        // console.log(s.length);

        // 找到 字符d, 第一次出现的位置
        // var n = s.indexOf('d');
        // console.log(n);

        /**
         * 截取 字符串
         * substr() 方法返回一个字符串中从 指定位置 开始到 指定字符数 的字符
         */
        // var n = s.substr(1, 3);
        // console.log(n); // expected output jaw


        /**
         * 字符串中的 大写, 全部转为 小写
         * 字符串中的 小写, 全部转为 大写
         */
        // var sc = "JavaScript";
        // console.log(sc.toLowerCase()); // expected output "javascript"
        // console.log(sc.toUpperCase()); // expected output "JAVASCRIPT"

        /**
         * 替换字符串 Java -> TypeScript
         * 如果pattern是字符串,则仅替换第一个匹配项
         * replace(pattern, replacement): replacement: 替换后的值
         */
        // var sc = "JavaScriptJavaScript";
        // console.log(sc.replace("Java", "Type")); // expected output: "TypeScriptTypeScript"
        // // actural output: "TypeScriptJavaScript"

        // // 仅替换 第1个
        // const regex = /Java/i;
        // console.log(sc.replace(regex, 'Type')); // acturally output: "TypeScriptJavaScript"

        // // 替换所有, 全局替换; g: 全局
        // const regex = /Java/g;
        // console.log(sc.replace(regex, 'Type')); // acturally output: "TypeScriptTypeScript"


        // const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
        //
        // console.log(p.replace('dog', 'monkey'));
        // // expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
        //
        //
        // const regex = /Dog/i;
        // console.log(p.replace(regex, 'ferret'));
        // // expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"


        // 删除字符串2边的 空白字符
        var sentence = "  javascript/typescript  ";
        console.log(sentence.length);

        var newSentence = sentence.trim();

        console.log(newSentence); // expected output: "javascript/typescript"
        console.log(newSentence.length);



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

posted on 2022-07-31 15:34  beyondx  阅读(8)  评论(0编辑  收藏  举报

导航