How to remove N characters from the middle of a string in JavaScript

1 Answer

0 votes
let str = "abc123def";
 
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:
  
abcdef
  
*/

 



answered Sep 11, 2024 by avibootz
edited Sep 11, 2024 by avibootz

Related questions

2 answers 198 views
1 answer 122 views
1 answer 138 views
1 answer 131 views
...