xxxxxxxxxx
function toTitleCase(str) {
return str.replace('_', ' ').replace(/(?:^|\s)\w/g, function(match) {
return match.toUpperCase();
});
}
var title = "hello world"
var titleWithUnderscores = "hello_world"
console.log(toTitleCase(title)); // Hello World
console.log(toTitleCase(titleWithUnderscores)); // Hello World
xxxxxxxxxx
function titleCase(str) {
return str
.split(' ')
.map((word) => word[0].toUpperCase() + word.slice(1).toLowerCase())
.join(' ');
}
console.log(titleCase("I'm a little tea pot")); // I'm A Little tea Pot
xxxxxxxxxx
function titleCase(str) {
return str.toLowerCase().replace(/(^|\s)\S/g, L => L.toUpperCase());
}
xxxxxxxxxx
function titleCase(sentence) {
let sentence = string.toLowerCase().split(" ");
for (let i = 0; i < sentence.length; i++) {
sentence[i] = sentence[i][0].toUpperCase() + sentence[i].slice(1);
}
return sentence.join(" ");
}
xxxxxxxxxx
function toTitleCase(str) {
return str.toLowerCase().split(' ').map(function(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}).join(' ');
}
// Example usage
var sentence = "this is an example sentence";
var converted = toTitleCase(sentence);
console.log(converted); // Output: This Is An Example Sentence
xxxxxxxxxx
function toTitleCase(str) {
return str.toLowerCase().replace(/(^|\s)\w/g, function(letter) {
return letter.toUpperCase();
});
}
// Example usage
const sentence = "hello world";
console.log(toTitleCase(sentence)); // Output: Hello World