programming-examples/js/String/Insert a string within a specific position in another string.js
2019-11-15 12:59:38 +01:00

12 lines
481 B
JavaScript

insert = function insert(main_string, ins_string, pos) {
if(typeof(pos) == "undefined") {
pos = 0;
}
if(typeof(ins_string) == "undefined") {
ins_string = '';
}
return main_string.slice(0, pos) + ins_string + main_string.slice(pos);
}
console.log(insert('We are doing some exercises.'));
console.log(insert('We are doing some exercises.','JavaScript '));
console.log(insert('We are doing some exercises.','JavaScript ',18));