我正在解決CoderByte上的一些問題。這個函數的目標是獲取一個字符串並將每個單詞的首字母大寫。例如,'hello world'
至'Hello World'
。我不明白爲什麼這段代碼給我'hello world'
而不是'Hello World'
。我知道這行array[i][0] = array[i][0].toUpperCase();
是問題,但不明白爲什麼它不工作。我想出了一個替代解決方案,但我想知道爲什麼這不起作用。非常感謝你的幫助!更改數組中的字符串 - Javascript
function LetterCapitalize(str) {
var array = str.split(' ');
for (var i = 0; i < array.length; i++) {
array[i][0] = array[i][0].toUpperCase();
}
return array.join('');
}
LetterCapitalize('hello world');
看到 http://stackoverflow.com/questions/4878756/javascript-如何對大寫字母進行首字母縮寫 - 每個字都像一個2字的城市 – Sane
var upperCased = str.split(「」).map(function(item){return item.charAt(0 ).toUpperCase()+ item.substr(1)})。join(「」) – JazzCat
[如何在JavaScript中使字符串大寫的第一個字母?](http://stackoverflow.com/questions/1026069 /怎麼辦 - 我 - 讓最首字母的-A-串上case-in-javascript) – Sreekanth