JS String对象

<!DOCTYPE html>

<html lang=”en”>

<head>

    <meta charset=”UTF-8″>

    <title>String对象</title>

</head> <body>

    <script>

        //构建String对象

        //赋值

        var str1 = “haol”

        //输出类型

        document.write(typeof(str1));

        document.write(“<br/>”)

        //输出属性:可以查看字符串的长度

        document.write(str1.length);

        document.write(“<br/>”)

        var str3 = “ABCDE”

        document.write(str3.toLowerCase()); //将字母转换成小写字母tolowerCase

        document.write(“<br/>”)

        var str4 = “abcdef”

        document.write(str4.toUpperCase()); //将字母转换成大写字母toUpperCase

        document.write(“<br/>”)

        //截取字符串,字符串从0开始编号

        document.write(str4.substr(1, 5));

        document.write(“<br/>”);

        //如果截取位为负数  则从字符串尾部开始

        document.write(str4.substr(-2));

        document.write(“<br/>”)

        //如果截取位只有一个值,那么就会从当前值往后截取

        document.write(str4.substr(2))

        document.write(“<br/>”)

        //字符串反转

        var str5 = “abcdefg”;

        var temp = “”;

        for (var i = str5.length – 1; i >= 0; i–) {             temp += str5.substr(i, 1);

        }

        document.write(temp);

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