JavaScript toUpperCase isn't working. Why? -
i doing simple function. turn words first-letter upper case, doesn't work, neither display errors:
function formattitle(input) { var words = input.split(' '); (var = 0; < words.length; i++) { words[i][0] = words[i][0].touppercase(); }; return words.join(' '); }; var newtitle = formattitle("all words first-letter should upper case"); document.write(newtitle);
thanks in advance.
the problem strings in javascript immutable. can't change char this.
a solution this:
words[i] = words[i][0].touppercase()+words[i].slice(1);
but have simpler , faster code using regular expression:
return input.replace(/\b\w/g,function(b){ return b.touppercase() })
(here more complete uppercasing, not after spaces - if want stick spaces use replace(/(\s+|^)\w/g,function(b){ return b.touppercase() })
)
Comments
Post a Comment