How to split a string at the first occurrence of a separator in TypeScript

1 Answer

0 votes
let str: string = "java go c c++ typescript python c#";
 
let pos: number = str.indexOf("c");
 
let part1: string = "", part2: string = "";
 
if (pos !== -1) {
    part1 = str.substring(0, pos);
    part2 = str.substring(pos);
}
 
console.log(part1); 
console.log(part2);
 
  
             
   
/*
run:
     
"java go " 
"c c++ typescript 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
...