Insert a string at a specific index

前端 未结 18 875
眼角桃花
眼角桃花 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:20

    my_string          = "hello world";
    my_insert          = " dear";
    my_insert_location = 5;
    
    my_string = my_string.split('');  
    my_string.splice( my_insert_location , 0, my_insert );
    my_string = my_string.join('');
    

    https://jsfiddle.net/gaby_de_wilde/wz69nw9k/

提交回复
热议问题