How to add N spaces to middle of string before specific letter in Node.js

1 Answer

0 votes
let str = 'nodejs java c c++ php';
 
const N = 5;

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

str = str.slice(0, index) + ' '.repeat(N) + str.slice(index);
 
console.log(str); 
 
    
    
    
    
/*
run:
    
node     js java c c++ php

*/

 



answered Apr 26, 2022 by avibootz

Related questions

1 answer 111 views
1 answer 106 views
1 answer 117 views
1 answer 113 views
2 answers 132 views
...