In Angular, you can make the first letter of a string uppercase using the toUpperCase()
method and string concatenation. Here's an example code snippet:
pythonlet str = "hello world";
let capitalizedStr = str.charAt(0).toUpperCase() + str.slice(1);
console.log(capitalizedStr); // "Hello world"
In this code, we first declare a string str
with the value "hello world"
. Then, we create a new string capitalizedStr
by concatenating the first letter of the original string, which is converted to uppercase using the toUpperCase()
method, with the rest of the original string, which is obtained using the slice()
method starting from the second character.
You can also use the ES6 arrow function to create a reusable function to capitalize the first letter of any given string as follows:
pythonconst capitalizeFirstLetter = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
console.log(capitalizeFirstLetter("hello world")); // "Hello world"
console.log(capitalizeFirstLetter("foo bar")); // "Foo bar"
This function takes a string as an argument and returns a new string with the first letter capitalized.
Comments
Post a Comment