每日一题:将字符串转换驼峰式的函数

  • 第一种:使用字符串的replace方法
    let str = 'a nice girl';
    let upperStr = str.replace(/\s+([^\s]*)/g,function($0,$1){
       return $1.substring(0,1).toUpperCase()+$1.substring(1);
    })
    console.log(upperStr()) //aNiceGirl
复制代码
  • 第二种:先将字符串转成数组,遍历数组转换,再拼成字符串
    let a =  'hello world' ;
    var b=a.split(' ').map(item=>{
        return item[0].toUpperCase()+item.substr(1,item.length)
    }).join('')
    console.log(b)//helloWrold
复制代码

猜你喜欢

转载自blog.csdn.net/weixin_34273046/article/details/87964205