How to capitalize the first letter of each word in a string with TypeScript

1 Answer

0 votes
function toTitleCase(s : string ) : string {
   let words_array = s.toLowerCase().split(' ');

   for (var i = 0; i < words_array.length; i++) {
      words_array[i] = words_array[i].charAt(0).toUpperCase() + words_array[i].substring(1);
   }
   
   return words_array.join(' ');
}


let s = "TypeScript is a programming language developed and maintained by Microsoft";

s = toTitleCase(s);

console.log(s);






/*
run:

"Typescript Is A Programming Language Developed And Maintained By Microsoft" 

*/

 



answered Jan 20, 2022 by avibootz

Related questions

...