Insert a string at a specific index

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

    another solution, cut the string in 2 and put a string in between.

    var str = jQuery('#selector').text();
    
    var strlength = str.length;
    
    strf = str.substr(0 , strlength - 5);
    strb = str.substr(strlength - 5 , 5);
    
    jQuery('#selector').html(strf + 'inserted' + strb);
    

提交回复
热议问题