Capitalize the first letter of every word:
xxxxxxxxxx
let capitalizeFirstLetter = function (sentence) {
let wordArray = sentence.toLowerCase().split(" ");
let newSentence = [];
for (let word of wordArray) {
let capWord = word[0].toUpperCase() + word.substring(1);
newSentence.push(capWord);
}
return newSentence.join(" ");
}
xxxxxxxxxx
const uppercaseWords = str => str.replace(/^(.)|\s+(.)/g, c => c.toUpperCase());
// Example
uppercaseWords('hello world'); // 'Hello World'
xxxxxxxxxx
text.replace(/(^\w|\s\w)/g, m => m.toUpperCase());
// Explanation:
//
// ^\w : first character of the string
// | : or
// \s\w : first character after whitespace
// (^\w|\s\w) Capture the pattern.
// g Flag: Match all occurrences.
// Example usage:
// Create a reusable function:
const toTitleCase = str => str.replace(/(^\w|\s\w)/g, m => m.toUpperCase());
// Call the function:
const myStringInTitleCase = toTitleCase(myString);
xxxxxxxxxx
const toTitleCase = (phrase) => {
return phrase
.toLowerCase()
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join(' ');
};
let result = toTitleCase('maRy hAd a lIttLe LaMb');
console.log(result);
xxxxxxxxxx
const lower = 'this is an entirely lowercase string';
const upper = lower.charAt(0).toUpperCase() + lower.substring(1);
xxxxxxxxxx
const str = 'captain picard';
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
const caps = str.split(' ').map(capitalize).join(' ');
caps; // 'Captain Picard'
xxxxxxxxxx
const str = 'FIDEURAM VITA';
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
const caps = str.split(' ').map(capitalize).join(' ');
caps; // 'Fideuram Vita'
xxxxxxxxxx
function titleCase(str) {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
// You do not need to check if i is larger than splitStr length, as your for does that for you
// Assign it back to the array
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(' ');
}
document.write(titleCase("I'm a little tea pot"));
xxxxxxxxxx
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
xxxxxxxxxx
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}