Hi Friends đź‘‹,
Welcome to Infinitbility ❤️!
Today, we are going to learn how to convert string in any case like camel case, paskal case to title case in typescript, here we will create toTitleCase()
custom method to convert string any case to title case.
Well, let’s create a function which return the provided string with the first letter of each word capitalized and Make sure the rest of the word is in lower case.
let’s dive in code…
1/**2 * Convert string to title case3 *4 * @param str5 * @returns title case string6 */7const toTitleCase = (str: string) => {8 return str.toLowerCase().split(' ').map(function(word) {9 return word.replace(word[0], word[0].toUpperCase());10 }).join(' ');11}1213toTitleCase(`I'm a infinitbility`);14toTitleCase(`WELCOME TO INFINITBILITY`);
Well, when you run above code, you can able to see above string converted in the title case.
For now, let’s check output.
Output

All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.