【JS】字符串练习

Osinghong2个月前 (04-28)成果展示69
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>d24c04429</title>
</head>
<body>
    <script>
    const str = "abaasdffggghhjjkkgfddsssss3444343";
    console.log("字符串长度:", str.length);

    const positions = [0, 3, 5, 9];
    positions.forEach(pos => {
        console.log(`位置 ${pos} 的字符:`, str.charAt(pos));
    });

    const find = ['i', 'c', 'b'];
    find.forEach(char => {
        console.log(`字符 "${char}" 是否存在:`, str.includes(char));
    });

    const replacedStr1 = str.replace(/g/g, "22"); 
    console.log("替换 g 为 22 后的字符串:", replacedStr1);

    const replacedStr2 = str.replace(/ss/g, "b");
    console.log("替换 ss 为 b 后的字符串:", replacedStr2);

    const start = 1, end = 5;
    const substring = str.substring(start, end);
    console.log(`从位置 ${start} 到 ${end} 的子字符串:`, substring);
    </script>
</body>
</html>


打赏
分享给朋友:

相关文章

【H5】页面布局练习6个月前 (12-05)
【JS】水果查询3个月前 (03-17)
【JS】For语句3个月前 (03-17)
【JS】For语句练习3个月前 (03-17)
【JS】While循环3个月前 (03-24)