How to remove N characters from the middle of a string in Node.js

1 Answer

0 votes
let str = "abcd123efgi";
  
const mid = Math.floor(str.length / 2);
const N = 3;
  
str = str.substring(0, mid - Math.floor(N / 2)) + str.substring(mid + 1 + Math.floor(N / 2));
  
console.log(str);
  
  
   
/*
run:
   
abcdefgi
   
*/

 



answered Sep 11, 2024 by avibootz

Related questions

2 answers 131 views
1 answer 139 views
1 answer 106 views
2 answers 128 views
1 answer 102 views
...