H5^Study

JS基础学习:String常用的属性

2019-03-30  本文已影响0人  Merbng

String常用属性

<script type="text/javascript">
            var str = "x1卡卡卡";
            console.log("长度:" + str.length);
            console.log("charAt:" + str.charAt(0));
            console.log("fromCharCode:" + String.fromCharCode(89, 88));
            // 字符串拼接 返回的是一个新的字符串
            var concatStr = str.concat(",啊,");
            console.log("拼接结果:" + concatStr);

            var str = "俺们那卡挖爱我的关系看过吗";
            // 从索引5的位置开始提取,到索引为10的前一个结束,没有10,并返回这个提取后的字符串
            var str = str.slice(5, 10);
            console.log("slice:" + str);

            var str = "二分法|新工人房恩爱|恩爱|环境|看看|新闻|好像|来了";
            var arr = str.split("|", 1); //第二个参数是 切几个
            for (i = 0; i < arr.length; i++) {
                console.log(arr[i]);
            }
            console.log(str);
            console.log("lastIndexOf:" + str.lastIndexOf("好"));
            console.log("replace:" + str.replace("来了", "xx"));
            console.log("substr:" + str.substr(5, 3));
            console.log("substring:" + str.substring(5, 7));
            console.log("indexOf:" + str.indexOf("恩爱", 5));
            console.log("lastIndexOf:" + str.lastIndexOf("恩爱"));

            var str = "阿均线爱我,瓦莉拉空格,啊";
            if (str.indexOf("我") != -1) {
                str = str.replace("我", "xx");
                console.log("str:" + str);
            } else {
                console.log("不存在");
            }

            var str = "Hello";
            console.log("toUpperCase:" + str.toUpperCase());
            console.log("toLowerCase:" + str.toLowerCase());

            var str = "                   这么美                   ";
            console.log("trim:" + str.trim());

            var str = "我,,付款毛线帽";
            var key = "付款";
            var index = str.indexOf(key);
            str = str.substr(index, 2);
            console.log("str:" + str)

            // 找出o出现的位置
            var str = "wo xxo xxxxxxo oxxxxxxxxxxxxxxxxo";
            var index = 0;
            var key = "o";
            while ((index = str.indexOf(key, index)) != -1) {
                console.log(index);
                index += key.length;
            }
        </script>

demo地址

上一篇下一篇

猜你喜欢

热点阅读