How to split a string at a specific index in TypeScript

1 Answer

0 votes
function split_by_index(str : string, index : number) {
  	const result = [str.slice(0, index), str.slice(index)];

 	 return result;
}

const str = "TypeScript Python C C++";

const [part1, part2] = split_by_index(str, 5);

console.log(part1); 
console.log(part2); 

 
 
 
/*
run:
 
"TypeS" 
"cript Python C C++" 

*/

 



answered May 1, 2022 by avibootz
...