题解 | #将字符串转换为驼峰格式#
将字符串转换为驼峰格式
http://www.nowcoder.com/practice/2ded24e34ec34325a62d42d0c8479bae
简单直接的解法,第一次提交的时候没有考虑到短横线在第一个。
所以加了一个判断,如果在第一个,就将设置的序号往后延一个
function cssStyle(sName) {
let firstName = sName.split("-"); // [ '', 'webkit', 'background', 'composite' ]
let results = '';
if (firstName[0] !== "") {
for (var i = 1; i < firstName.length; i++) {
let lastName = firstName[i].split(""); //转换为数组,便于使用splice方法,进行首字母设置
lastName.splice(0, 1, lastName[0].toUpperCase()) //将第一个字母用大写字母进行替换
let result = lastName.join(""); //将首字母变为大写的数组转换回字符串。
results += result;
}
return firstName[0] + results; //拼接字符串(短横线不在第一位时)
} else {
for (var i = 2; i < firstName.length; i++) {
let lastName = firstName[i].split(""); //转换为数组,便于使用splice方法,进行首字母设置
lastName.splice(0, 1, lastName[0].toUpperCase()) //将第一个字母用大写字母进行替换
result = lastName.join(""); //将首字母变为大写的数组转换回字符串。
results += result;
}
return firstName[1] + results; //拼接字符串(短横线在第一位时)
}
}