Insert a string at a specific index

前端 未结 18 912
眼角桃花
眼角桃花 2020-11-22 14:02

How can I insert a string at a specific index of another string?

 var txt1 = \"foo baz\"

Suppose I want to insert \"bar \" after the \"foo

18条回答
  •  暖寄归人
    2020-11-22 14:31

    Inserting at a specific index (rather than, say, at the first space character) has to use string slicing/substring:

    var txt2 = txt1.slice(0, 3) + "bar" + txt1.slice(3);
    

提交回复
热议问题