每个单词首字符转为大写

function capitalize(string) {
  var words = string.split(" ");
  for (var i = 0; i < words.length; i++) {
    words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
  }
  return words.join(" ");

}
var string = "I believe I am the best";
console.log(capitalize(string));

你可能感兴趣的:(每个单词首字符转为大写)