CAN'T Use 'toUpperCase' (JS)
This question already has an answer here:
The best method I've found is just to call toUpperCase
on the first character and concat the rest of the string using slice
:
function capitalize(str) {
if(typeof str === 'string') {
return str[0].toUpperCase() + str.slice(1);
}
return str;
}
If you want to capitalize each word in a sentence, you can split on space:
"capitalize each word of this sentence".split(' ').map(capitalize).join(' ');
链接地址: http://www.djcxy.com/p/17870.html