How to split a string at a specific index in Node.js

1 Answer

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

 	return result;
}

const str = "NodeJS Python C C++ PHP";

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

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


 
 
 
/*
run:
 
NodeJS Py
thon C C++ PHP

*/

 



answered May 1, 2022 by avibootz

Related questions

...