TypeScript:
Capitalizing a string
How to:
TypeScript, being a superset of JavaScript, allows for various methods to capitalize strings, ranging from pure JavaScript approaches to utilizing third-party libraries for more complex or specific use cases.
Pure JavaScript Approach:
function capitalize(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
// Sample Output:
console.log(capitalize('hello TypeScript!')); // 'Hello TypeScript!'
This method is straightforward and relies on the charAt()
method to access the first character of the string and toUpperCase()
to convert it to uppercase. The slice(1)
method then retrieves the rest of the string, leaving it unchanged.
Using Lodash Library:
For projects already using the Lodash library, you can utilize its _.capitalize
function to achieve the same result with less boilerplate code.
First, install Lodash:
npm install lodash
Then, use it in your TypeScript file:
import * as _ from 'lodash';
// Sample Output:
console.log(_.capitalize('hello TypeScript!')); // 'Hello typescript!'
Note: Lodash’s _.capitalize
method converts the rest of the string to lowercase which might not always be what you want.
Using a Regular Expression:
A regular expression can provide a concise way to capitalize the first letter of a string, especially if you need to capitalize the first letter of each word in a string.
function capitalizeWords(str: string): string {
return str.replace(/\b\w/g, char => char.toUpperCase());
}
// Sample Output:
console.log(capitalizeWords('hello typescript world!')); // 'Hello Typescript World!'
This method uses the replace()
function to search for any word boundary followed by an alphanumeric character (\b\w
), capitalizing each match. It’s particularly handy for titles or headings.