📌  相关文章
📜  javascript 将每个单词的首字母大写 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:04:21.833000             🧑  作者: Mango

代码示例6
function titleCase(str) {
   var splitStr = str.toLowerCase().split(' ');
   for (var i = 0; i < splitStr.length; i++) {
       // You do not need to check if i is larger than splitStr length, as your for does that for you
       // Assign it back to the array
       splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);     
   }
   // Directly return the joined string
   return splitStr.join(' '); 
}

document.write(titleCase("I'm a little tea pot"));