How to add N spaces to middle of string before specific letter in JavaScript

1 Answer

0 votes
let str = 'javascript java typescript c c++ php';

const N = 5;

const index = str.indexOf('t');

str = str.slice(0, index) + ' '.repeat(N) + str.slice(index);
 
console.log(str); 
 
    
    
    
    
/*
run:
    
"javascrip     t java typescript c c++ php"
    
*/

 



answered Apr 26, 2022 by avibootz

Related questions

1 answer 128 views
1 answer 118 views
1 answer 109 views
1 answer 124 views
...