不能使用'toUpperCase'(JS)
这个问题在这里已经有了答案:
我发现的最好的方法就是在第一个字符上调用toUpperCase
,并使用slice
对字符串的其余部分进行连接:
function capitalize(str) {
if(typeof str === 'string') {
return str[0].toUpperCase() + str.slice(1);
}
return str;
}
如果你想在句子中大写每个单词,你可以分割空间:
"capitalize each word of this sentence".split(' ').map(capitalize).join(' ');
链接地址: http://www.djcxy.com/p/17869.html