How to split a string in half but not in the middle of a word with Node.js

1 Answer

0 votes
const str = "c# c node.js php c++ python go";

const halfstr = str.substring(0, (parseInt(str.length / 2) + 1));
const center = halfstr.lastIndexOf(' ') + 1;
        
const parts = [str.substring(0, center), str.substring(center)];
        
console.log(parts[0]);
console.log(parts[1]);





/*
run:

c# c node.js 
php c++ python go

*/

 



answered Aug 10, 2023 by avibootz
edited Sep 10, 2024 by avibootz
...