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 214 views
2 answers 273 views
3 answers 200 views
1 answer 185 views
2 answers 261 views
3 answers 275 views
...