1//capitalize only the first letter of the string.
2function capitalizeFirstLetter(string) {
3 return string.charAt(0).toUpperCase() + string.slice(1);
4}
5//capitalize all words of a string.
6function capitalizeWords(string) {
7 return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
8};
1function toTitleCase(str) {
2 return str.replace(/\w\S*/g, function(txt){
3 return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
4 });
5}
1//Updated
2//capitalize only the first letter of the string.
3function capitalizeFirstLetter(string) {
4 return string.charAt(0).toUpperCase() + string.slice(1);
5}
6//capitalize all words of a string.
7function capitalizeWords(string) {
8 return string.replace(/(?:^|\s)\S/g, function(a) { return a.toUpperCase(); });
9};
1const capitalizeFirstLetter(string) =>
2 string.charAt(0).toUpperCase() + string.slice(1).toLowerCase()
1export function capitalize(str: string, all: boolean = false) {
2 if (all)
3 return str.split(' ').map(s => capitalize(s)).join(' ');
4 return str.charAt(0).toUpperCase() + str.slice(1);
5}