How to count the occurrences of a word in a string using TypeScript

1 Answer

0 votes
function countOccurences(s: string, word: string) : number { 
    return s.split(word).length - 1
} 
     
const s: string = "typescript c python php c++ typescript php c# typescript typescript"; 
const word: string = "typescript"; 
   
console.log(countOccurences(s, word)); 
  
     
/*
run:
     
4
    
*/

 



answered Feb 28, 2025 by avibootz

Related questions

...