字符串函数的运用

<!DOCTYPE html>
<html lang="zh - CN">

<head>
    <meta charset="UTF - 8">
    <meta name="viewport" content="width=device-width, initial - scale=1.0">
    <title>字符串函数示例</title>
</head>

<body>
    <script>
        // charAt() 示例
        let str1 = "Hello";
        console.log("charAt() 示例:" + str1.charAt(1));

        // charCodeAt() 示例
        let str2 = "World";
        console.log("charCodeAt() 示例:" + str2.charCodeAt(0));

        // concat() 示例
        let str3 = "Hello";
        let str4 = " World";
        console.log("concat() 示例:" + str3.concat(str4));

        // fromCharCode() 示例
        console.log("fromCharCode() 示例:" + String.fromCharCode(65, 66, 67));

        // indexOf() 示例
        let str5 = "abcabc";
        console.log("indexOf() 示例:" + str5.indexOf('b'));

        // lastIndexOf() 示例
        console.log("lastIndexOf() 示例:" + str5.lastIndexOf('b'));

        // localeCompare() 示例
        let str6 = "apple";
        let str7 = "banana";
        console.log("localeCompare() 示例:" + str6.localeCompare(str7));

        // match() 示例
        let str8 = "I love JavaScript";
        console.log("match() 示例:" + str8.match(/love/));

        // replace() 示例
        let str9 = "The cat is cute";
        console.log("replace() 示例:" + str9.replace("cat", "dog"));

        // search() 示例
        let str10 = "Find the keyword";
        console.log("search() 示例:" + str10.search(/keyword/));

        // slice() 示例
        let str11 = "JavaScript is fun";
        console.log("slice() 示例:" + str11.slice(0, 10));

        // split() 示例
        let str12 = "apple,banana,cherry";
        console.log("split() 示例:" + str12.split(','));

        // substr() 示例
        let str13 = "Substring example";
        console.log("substr() 示例:" + str13.substr(0, 5));

        // substring() 示例
        let str14 = "Another substring example";
        console.log("substring() 示例:" + str14.substring(0, 5));

        // toLocaleLowerCase() 示例
        let str15 = "HELLO";
        console.log("toLocaleLowerCase() 示例:" + str15.toLocaleLowerCase());

        // toLocaleUpperCase() 示例
        let str16 = "hello";
        console.log("toLocaleUpperCase() 示例:" + str16.toLocaleUpperCase());

        // toLowerCase() 示例
        let str17 = "UpperCase";
        console.log("toLowerCase() 示例:" + str17.toLowerCase());

        // toString() 示例
        let num = 123;
        console.log("toString() 示例:" + num.toString());

        // toUpperCase() 示例
        let str18 = "lowercase";
        console.log("toUpperCase() 示例:" + str18.toUpperCase());

        // trim() 示例
        let str19 = "   Trim me   ";
        console.log("trim() 示例:" + str19.trim());

        // valueOf() 示例
        let strObj = new String("ValueOf example");
        console.log("valueOf() 示例:" + strObj.valueOf());
    </script>
</body>

</html>

 

posted @ 2025-11-23 09:38  爱吃泡面的皮卡  阅读(8)  评论(0)    收藏  举报