我该怎么做?我希望字符串的第一个字符是大写的,其余的是小写的



我希望字符串的第一个字符为大写,其余为小写这是我对的编码

let title = prompt().toLowerCase();
for (let c of title)
{
c[0]=c[0].toUpperCase();
}
  • 有很多方法,下面只是其中之一
function toUpperCaseFirstLetter(str) {
if (typeof str !== 'string' || str.length === 0) {
return str;
}
return str[0].toUpperCase() + str.slice(1).toLowerCase();
}
// some tests
expect(toUpperCaseFirstLetter('abc')).eql('Abc');
expect(toUpperCaseFirstLetter('ABC')).eql('Abc');
expect(toUpperCaseFirstLetter('')).eql('');
// other tests....

相关内容

最新更新