將hello-world格式轉爲駝峯格式 helloWorld

  • 以橫線爲分隔符,將字符串分割成數組
  • 從數組的第二項開始遍歷
  • 每次遍歷時,選中當前項中的第一個字符,將其轉爲大寫,以後拼接上當前項其他的內容
  • 將改變後的內容賦值給當前正在遍歷的項
  • 最後將數組轉換成字符串
var str = 'hello-world-abc';
var strSplit = str.split('-');
for(var i=1;i<strSplit.length;i++){
  strSplit[i] = strSplit[i][0].toUpperCase()+strSplit[i].slice(1);
}
var res = strSplit.join('');
console.log(res);
相關文章
相關標籤/搜索