How to insert char in a string at specific position with JavaScript

1 Answer

0 votes
insert_char = function insert_char(s, ch, pos) {
    return s.slice(0, pos) + ch + s.slice(pos);
}

   
s = "PythonJavaScriptC++"; 

s = insert_char(s, " ", 6);
s = insert_char(s, "W", 12);
         
document.write(s);   

 
 
     
/*
run:
 
Python JavaSWcriptC++ 
          
*/

 



answered Jan 16, 2020 by avibootz

Related questions

1 answer 210 views
2 answers 269 views
3 answers 195 views
1 answer 182 views
2 answers 258 views
3 answers 273 views
...