标题案例A句子JavaScript
假设我们需要编写一个函数,该函数接受一个字符串并对该字符串中每个单词的首字母大写,并将所有其余字母的大小写更改为小写。
例如,如果输入字符串是-
hello world coding is very interesting
输出应为-
Hello World Coding Is Very Interesting
让我们定义一个函数capitaliseTitle(),它接受一个字符串并将每个单词的首字母大写并返回字符串-
示例
let str = 'hello world coding is very interesting';
const capitaliseTitle = (str) => {
const string = str
.toLowerCase()
.split(" ")
.map(word => {
return word[0]
.toUpperCase() + word.substr(1, word.length);
})
.join(" ");
return string;
}
console.log(capitaliseTitle(str));输出结果
控制台输出将是-
Hello World Coding Is Very Interesting
热门推荐
3 十月简短祝福语
10 婚庆花篮祝福语简短大气
11 国庆中秋祝福语简短搞笑
12 我想找祝福语大全简短
13 送笔筒的祝福语简短
14 职场离职文案祝福语简短
15 老师对学校祝福语简短
16 生病祝福语简短文案
17 公司开年仪式祝福语简短
18 简短的出行工作祝福语