How to remove the last comma from the end of string in TypeScript

2 Answers

0 votes
let str = 'typescript,c,c++,python,';
 
str = str.replace(/,*$/, '');
 
console.log(str); 
 
   
   
   
   
/*
run:
   
"typescript,c,c++,python"
   
*/

 



answered Apr 12, 2022 by avibootz
edited Jun 9, 2022 by avibootz
0 votes
let str = 'typescript,c,c++,python,';
 
if (str.endsWith(',')) {
    str = str.slice(0, -1);
}
 
console.log(str); 
 
   
   
   
   
/*
run:
   
"typescript,c,c++,python"
   
*/

 



answered Jun 9, 2022 by avibootz

Related questions

1 answer 136 views
1 answer 195 views
1 answer 165 views
1 answer 149 views
1 answer 151 views
1 answer 160 views
...