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

1 Answer

0 votes
let str = "java go c c++ node.js python c#";
 
let pos = str.indexOf("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 c++ node.js python c#
       
*/

 



answered May 23, 2024 by avibootz

Related questions

1 answer 112 views
1 answer 122 views
1 answer 141 views
1 answer 179 views
2 answers 228 views
...