How to split a string in half but not in the middle of a word with TypeScript

1 Answer

0 votes
const str: string  = "c# c c++ php typescript python go";

const halfstr = str.substring(0, (Math.floor(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 c++ php " 
"typescript python go" 

*/
 

 



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