How to remove the last N words from a string in TypeScript

1 Answer

0 votes
function removeTheLastNWordsFromString(str : string, total : number) : string {
	return str.split(' ').slice(0, -total).join(' ');
}

let str = 'javascript typescript node.js c c++';
const N = 3;

str = removeTheLastNWordsFromString(str, N);

console.log(str);


  
  
  
  
/*
run:

"javascript typescript" 
  
*/

 



answered Jun 11, 2022 by avibootz
...