首页 > 其他 > 详细

字符串转化成驼峰命名

时间:2020-01-06 18:17:30      阅读:109      评论:0      收藏:0      [点我收藏+]
    function convertToCamelCase (str) {
        var returnStr;
        if (str.charAt(0) === ‘-‘) {
            returnStr = str.substring(1).split(‘-‘)
        } else {
            returnStr = str.split(‘-‘)
        }
        console.log(returnStr)
        for (var i = 1; i<returnStr.length; i++) {
            returnStr[i] = returnStr[i][0].toUpperCase() + returnStr[i].substring(1)
        }
        return returnStr.join(‘‘)
    }
    console.log(convertToCamelCase(‘background-img-img2‘))

  方法二:

    function convertToCamelCase (str) {
        var returnStr = str.split(‘-‘);
        if (returnStr[0] === ‘‘) {
            returnStr.shift()
        }
        for (var i = 1; i<returnStr.length; i++) {
           if (returnStr[i] !== ‘‘) {
               returnStr[i] = returnStr[i][0].toUpperCase() + returnStr[i].substring(1)
           }
        }
        return returnStr.join(‘‘)
    }
    console.log(convertToCamelCase(‘-background-img-cas‘))

  

字符串转化成驼峰命名

原文:https://www.cnblogs.com/ly-qingqiu/p/12157430.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!