xxxxxxxxxx
function capitalizeFirstLetter(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
const input = 'hello world';
const output = capitalizeFirstLetter(input);
console.log(output); // Prints "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 lower = 'this is an entirely lowercase string';
const upper = lower.charAt(0).toUpperCase() + lower.substring(1);
xxxxxxxxxx
//Altered, "capitalize all words of a string" (javascript by Grepper on Jul 22 2019) answer to compatible with TypeScript
var myString = 'abcd abcd';
capitalizeWords(text){
return text.replace(/(?:^|\s)\S/g,(res)=>{ return res.toUpperCase();})
};
console.log(capitalizeWords(myString));
//result = "Abcd Abcd"
xxxxxxxxxx
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
xxxxxxxxxx
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
xxxxxxxxxx
// this will only capitalize the first word
var name = prompt("What is your name");
firstLetterUpper = name.slice(0,1).toUpperCase();
alert("Hello " + firstLetterUpper + name.slice(1, name.length).toLowerCase());
xxxxxxxxxx
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
console.log(capitalizeFirstLetter('foo')); // Foo
Run code snippet
xxxxxxxxxx
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.toLocaleLowerCase().substring(1)
}
capitalize("bob");
xxxxxxxxxx
const names = ["alice", "bob", "charlie", "danielle"]
// --> ["Alice", "Bob", "Charlie", "Danielle"]
//Just use some anonymous function and iterate through each of the elements in the array
//and take string as another array
let namescap = names.map((x)=>{
return x[0].toUpperCase()+x.slice(1)
})
console.log(namescap)