美文网首页
JavaScript封装函数进行字符串转换

JavaScript封装函数进行字符串转换

作者: 九瀺 | 来源:发表于2019-09-29 03:15 被阅读0次
 <script>
        // var foo = "get-element-by-id";
        // console.log(foo.split("-")[1].split("")[0].toUpperCase());
        // var arr = foo.split("-");
        // console.log(arr[1].charAt(0).toUpperCase()+arr[1].substr(1,arr[1].length));
        function strToStr(foo) {
            //利用split方法将字符串按相应格式切割转换成数组
            var arr = foo.split("-");
            for (var i = 1; i < arr.length; i++) {
                //将生成的字符重新赋值给切割后的数组中
                arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substr(1, arr[i].length)
            };
            //利用join方法将数组转换成字符串输出
            console.log(arr.join(""));
        }
        strToStr("get-element-by-id-do");
    </script>

相关文章

网友评论

      本文标题:JavaScript封装函数进行字符串转换

      本文链接:https://www.haomeiwen.com/subject/zopxpctx.html