将 var str = 'this-is-str';将'-'后的字母大写,即修改为thisIsStr的最简单方法是什么呢.
补充一下自己写的代码,感觉写的比较繁琐.
a = a.replace(a[0] , a[0].toUpperCase());
for(var i in a ){
if(a[i] === '-'){
a = a.replace(a[i] + a[parseInt(i) + 1] , a[parseInt(i) + 1].toUpperCase());
}
}
而且有一个疑问,为什么
typeof(i) == String;
根据楼下的代码,自己写成了reduce的形式,感觉其实没什么区别:
var a = "this-is-st".split('-');
a = a.reduce(function(pre,next){
pre += next.charAt(0).toUpperCase() + next.substr(1);
return pre;
},'');
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
感觉自己的也不是很简洁,不过可以通过正则表达式来实现。
是由于你遍历的是一个字符串这里说法错误,经过在chrome console中验证的确与类型无关。