sami sami - 4 months ago 9
jQuery Question

Inserting string at position x of another string

I have 2 vars and need to insert b into a at this position. The result I'm looking for is "I want an apple". How can I do this with jquery or javascript?

var a = "I want apple";
var b = "an";
var position = 6;

Answer

var a = "I want apple";
var b = "an";
var position = 6;
var output = [a.slice(0, position), b, a.slice(position)].join('');
console.log(output);

Comments