How to remove a given word from a string in TypeScript

1 Answer

0 votes
function removeWord(str : string, word : string) {
    if (str.includes(word)) {
        let tmp : string = word + " ";
        str = str.replace(tmp, "");
        tmp = " " + word;
        str = str.replace(tmp, "");
    }
    
    return str;
}
        
let str : string = "typescript c c++ c# python rust go";
const word : string = "rust";

str = removeWord(str, word);

console.log(str);





/*
run:

"typescript c c++ c# python go" 

*/

 



answered Nov 19, 2022 by avibootz
...