How to split a string at the last occurrence of a separator in Node.js

1 Answer

0 votes
let str = "java go c node.js c++ python";
  
let pos = str.lastIndexOf("c");
  
let part1 = "", part2 = "";
  
if (pos !== -1) {
    part1 = str.substring(0, pos);
    part2 = str.substring(pos);
}
  
console.log(part1); 
console.log(part2);
  
   
              
    
/*
run:
      
java go c node.js 
c++ python
        
*/

 



answered May 24, 2024 by avibootz
...