How to convert a string to title case in TypeScript

2 Answers

0 votes
function toTitleCase(s : string) {
    return s.toLowerCase().split(' ').map(function (word : string) {
            return (word.charAt(0).toUpperCase() + word.slice(1));
    }).join(' ');
}
 
console.log(toTitleCase("typescript is a programming language"));
  
  
    
      
      
/*
run:
      
"Typescript Is A Programming Language"
      
*/

 



answered May 24, 2022 by avibootz
0 votes
function toTitleCase(s : string) {
    return s.replace(/\w\S*/g, function (word : string) {
        return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
    }
  );
}
 
console.log(toTitleCase("typescript is a programming language"));
  
  
    
      
      
/*
run:
      
"Typescript Is A Programming Language" 
      
*/

 



answered May 24, 2022 by avibootz
...