xxxxxxxxxx
// via css
p::first-letter {
text-transform:capitalize;
}
// via function
function capitalizeString(string)
{
return string && string[0].toUpperCase() + string.slice(1);
}
capitalizeString('hello world') // return Hello world
// ES6
const capitalize = s => s ? s[0].toUpperCase() + s.slice(1) : ""
xxxxxxxxxx
//capitalize only the first letter of the string.
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
//capitalize all words of a string.
function capitalizeWords(string) {
return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
};
xxxxxxxxxx
export const toCapitalize = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
xxxxxxxxxx
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)
capitalize("follow for more")
// Result: Follow for more
xxxxxxxxxx
function capitalizeName(name) {
return name.replace(/\b(\w)/g, s => s.toUpperCase());
}
xxxxxxxxxx
const capitalize = s => s && s[0].toUpperCase() + s.slice(1)
// to always return type string event when s may be falsy other than empty-string
const capitalize = s => (s && s[0].toUpperCase() + s.slice(1)) || ""
xxxxxxxxxx
const Capitalize = function(string){
return string[0].toUpperCase + string.slice(1).toLowerCase;
}
xxxxxxxxxx
myString = 'the quick green alligator...';
myString.replace(/^\w/, (c) => c.toUpperCase());
myString = ' the quick green alligator...';
myString.trim().replace(/^\w/, (c) => c.toUpperCase());
xxxxxxxxxx
function capitalizeName(name) {
let nameObject = convertNameToObject(name);
let firstName = nameObject.firstName;
let lastName = nameObject.lastName;
return firstName[0].toUpperCase() + firstName.substring(1, firstName.length).toLowerCase() + " " + lastName[0].toUpperCase() + lastName.substring(1, lastName.length).toLowerCase()