标题大小写一个句子 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP