Replace part of string at specified position












0















I want to replace a part of string at specified position(start, end) by another string in javascript



Here is an example:



"Hello world this is a question"


I want to replace the part of this string starting at 5 and ending at 10 by "friends"



The output will be:



"Hello friends this is a question"









share|improve this question



























    0















    I want to replace a part of string at specified position(start, end) by another string in javascript



    Here is an example:



    "Hello world this is a question"


    I want to replace the part of this string starting at 5 and ending at 10 by "friends"



    The output will be:



    "Hello friends this is a question"









    share|improve this question

























      0












      0








      0








      I want to replace a part of string at specified position(start, end) by another string in javascript



      Here is an example:



      "Hello world this is a question"


      I want to replace the part of this string starting at 5 and ending at 10 by "friends"



      The output will be:



      "Hello friends this is a question"









      share|improve this question














      I want to replace a part of string at specified position(start, end) by another string in javascript



      Here is an example:



      "Hello world this is a question"


      I want to replace the part of this string starting at 5 and ending at 10 by "friends"



      The output will be:



      "Hello friends this is a question"






      javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 15:18









      Amadou BeyeAmadou Beye

      942515




      942515
























          5 Answers
          5






          active

          oldest

          votes


















          1














          For example with substring() calls and concatenation (+):






          var msg="Hello world this is a question";
          var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
          console.log(replaced);








          share|improve this answer
























          • accepted for the snippet ! Works great ! Thanks

            – Amadou Beye
            Nov 25 '18 at 15:31



















          1














          Method 1:



          If you know the exact index you want to slice the string at, you should probably use javascript string.slice method like so:



          var str = "Hello world!";
          var part = str.slice(1, 5);
          console.log(part); // "ello"


          Method 2:



          If you don't know the index, but you do know the string you want to replace, you can simply use string.replace method like so:



          var input = "Hello world this is a question";
          var result = input.replace("world", "friends");
          console.log(result); // Hello friends this is a question





          share|improve this answer

































            1














            You can use slice to achieve this.






            let str = "Hello world this is a question"

            function replace(st, en, val) {
            str = str.slice(0, st + 1) + val + str.slice(en + 1)
            }

            replace(5, 10, 'friends')
            console.log(str)








            share|improve this answer































              1














              You can try the replace() and substring() method






              var str = "Hello world this is a question";

              console.log(str.replace(str.substring(6, 11), "friends"));








              share|improve this answer

































                0














                there is replace function in javascript






                var replaceData = "Hello world this is a question";
                console.log(replaceData.replace('world', 'friends'));








                share|improve this answer
























                • the word to replace can appears multiple times and I only want to replace one

                  – Amadou Beye
                  Nov 25 '18 at 15:34











                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468915%2freplace-part-of-string-at-specified-position%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1














                For example with substring() calls and concatenation (+):






                var msg="Hello world this is a question";
                var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
                console.log(replaced);








                share|improve this answer
























                • accepted for the snippet ! Works great ! Thanks

                  – Amadou Beye
                  Nov 25 '18 at 15:31
















                1














                For example with substring() calls and concatenation (+):






                var msg="Hello world this is a question";
                var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
                console.log(replaced);








                share|improve this answer
























                • accepted for the snippet ! Works great ! Thanks

                  – Amadou Beye
                  Nov 25 '18 at 15:31














                1












                1








                1







                For example with substring() calls and concatenation (+):






                var msg="Hello world this is a question";
                var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
                console.log(replaced);








                share|improve this answer













                For example with substring() calls and concatenation (+):






                var msg="Hello world this is a question";
                var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
                console.log(replaced);








                var msg="Hello world this is a question";
                var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
                console.log(replaced);





                var msg="Hello world this is a question";
                var replaced=msg.substring(0,6)+"friends"+msg.substring(11);
                console.log(replaced);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 15:23









                tevemadartevemadar

                4,3382723




                4,3382723













                • accepted for the snippet ! Works great ! Thanks

                  – Amadou Beye
                  Nov 25 '18 at 15:31



















                • accepted for the snippet ! Works great ! Thanks

                  – Amadou Beye
                  Nov 25 '18 at 15:31

















                accepted for the snippet ! Works great ! Thanks

                – Amadou Beye
                Nov 25 '18 at 15:31





                accepted for the snippet ! Works great ! Thanks

                – Amadou Beye
                Nov 25 '18 at 15:31













                1














                Method 1:



                If you know the exact index you want to slice the string at, you should probably use javascript string.slice method like so:



                var str = "Hello world!";
                var part = str.slice(1, 5);
                console.log(part); // "ello"


                Method 2:



                If you don't know the index, but you do know the string you want to replace, you can simply use string.replace method like so:



                var input = "Hello world this is a question";
                var result = input.replace("world", "friends");
                console.log(result); // Hello friends this is a question





                share|improve this answer






























                  1














                  Method 1:



                  If you know the exact index you want to slice the string at, you should probably use javascript string.slice method like so:



                  var str = "Hello world!";
                  var part = str.slice(1, 5);
                  console.log(part); // "ello"


                  Method 2:



                  If you don't know the index, but you do know the string you want to replace, you can simply use string.replace method like so:



                  var input = "Hello world this is a question";
                  var result = input.replace("world", "friends");
                  console.log(result); // Hello friends this is a question





                  share|improve this answer




























                    1












                    1








                    1







                    Method 1:



                    If you know the exact index you want to slice the string at, you should probably use javascript string.slice method like so:



                    var str = "Hello world!";
                    var part = str.slice(1, 5);
                    console.log(part); // "ello"


                    Method 2:



                    If you don't know the index, but you do know the string you want to replace, you can simply use string.replace method like so:



                    var input = "Hello world this is a question";
                    var result = input.replace("world", "friends");
                    console.log(result); // Hello friends this is a question





                    share|improve this answer















                    Method 1:



                    If you know the exact index you want to slice the string at, you should probably use javascript string.slice method like so:



                    var str = "Hello world!";
                    var part = str.slice(1, 5);
                    console.log(part); // "ello"


                    Method 2:



                    If you don't know the index, but you do know the string you want to replace, you can simply use string.replace method like so:



                    var input = "Hello world this is a question";
                    var result = input.replace("world", "friends");
                    console.log(result); // Hello friends this is a question






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 25 '18 at 15:29

























                    answered Nov 25 '18 at 15:23









                    Ronen CypisRonen Cypis

                    14.7k11221




                    14.7k11221























                        1














                        You can use slice to achieve this.






                        let str = "Hello world this is a question"

                        function replace(st, en, val) {
                        str = str.slice(0, st + 1) + val + str.slice(en + 1)
                        }

                        replace(5, 10, 'friends')
                        console.log(str)








                        share|improve this answer




























                          1














                          You can use slice to achieve this.






                          let str = "Hello world this is a question"

                          function replace(st, en, val) {
                          str = str.slice(0, st + 1) + val + str.slice(en + 1)
                          }

                          replace(5, 10, 'friends')
                          console.log(str)








                          share|improve this answer


























                            1












                            1








                            1







                            You can use slice to achieve this.






                            let str = "Hello world this is a question"

                            function replace(st, en, val) {
                            str = str.slice(0, st + 1) + val + str.slice(en + 1)
                            }

                            replace(5, 10, 'friends')
                            console.log(str)








                            share|improve this answer













                            You can use slice to achieve this.






                            let str = "Hello world this is a question"

                            function replace(st, en, val) {
                            str = str.slice(0, st + 1) + val + str.slice(en + 1)
                            }

                            replace(5, 10, 'friends')
                            console.log(str)








                            let str = "Hello world this is a question"

                            function replace(st, en, val) {
                            str = str.slice(0, st + 1) + val + str.slice(en + 1)
                            }

                            replace(5, 10, 'friends')
                            console.log(str)





                            let str = "Hello world this is a question"

                            function replace(st, en, val) {
                            str = str.slice(0, st + 1) + val + str.slice(en + 1)
                            }

                            replace(5, 10, 'friends')
                            console.log(str)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 25 '18 at 15:29









                            Nitish NarangNitish Narang

                            2,9401815




                            2,9401815























                                1














                                You can try the replace() and substring() method






                                var str = "Hello world this is a question";

                                console.log(str.replace(str.substring(6, 11), "friends"));








                                share|improve this answer






























                                  1














                                  You can try the replace() and substring() method






                                  var str = "Hello world this is a question";

                                  console.log(str.replace(str.substring(6, 11), "friends"));








                                  share|improve this answer




























                                    1












                                    1








                                    1







                                    You can try the replace() and substring() method






                                    var str = "Hello world this is a question";

                                    console.log(str.replace(str.substring(6, 11), "friends"));








                                    share|improve this answer















                                    You can try the replace() and substring() method






                                    var str = "Hello world this is a question";

                                    console.log(str.replace(str.substring(6, 11), "friends"));








                                    var str = "Hello world this is a question";

                                    console.log(str.replace(str.substring(6, 11), "friends"));





                                    var str = "Hello world this is a question";

                                    console.log(str.replace(str.substring(6, 11), "friends"));






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Nov 25 '18 at 15:34









                                    Prashant Pimpale

                                    3,3273830




                                    3,3273830










                                    answered Nov 25 '18 at 15:24









                                    Doug FDoug F

                                    196210




                                    196210























                                        0














                                        there is replace function in javascript






                                        var replaceData = "Hello world this is a question";
                                        console.log(replaceData.replace('world', 'friends'));








                                        share|improve this answer
























                                        • the word to replace can appears multiple times and I only want to replace one

                                          – Amadou Beye
                                          Nov 25 '18 at 15:34
















                                        0














                                        there is replace function in javascript






                                        var replaceData = "Hello world this is a question";
                                        console.log(replaceData.replace('world', 'friends'));








                                        share|improve this answer
























                                        • the word to replace can appears multiple times and I only want to replace one

                                          – Amadou Beye
                                          Nov 25 '18 at 15:34














                                        0












                                        0








                                        0







                                        there is replace function in javascript






                                        var replaceData = "Hello world this is a question";
                                        console.log(replaceData.replace('world', 'friends'));








                                        share|improve this answer













                                        there is replace function in javascript






                                        var replaceData = "Hello world this is a question";
                                        console.log(replaceData.replace('world', 'friends'));








                                        var replaceData = "Hello world this is a question";
                                        console.log(replaceData.replace('world', 'friends'));





                                        var replaceData = "Hello world this is a question";
                                        console.log(replaceData.replace('world', 'friends'));






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 25 '18 at 15:25









                                        Krishna JonnalagaddaKrishna Jonnalagadda

                                        1,2341719




                                        1,2341719













                                        • the word to replace can appears multiple times and I only want to replace one

                                          – Amadou Beye
                                          Nov 25 '18 at 15:34



















                                        • the word to replace can appears multiple times and I only want to replace one

                                          – Amadou Beye
                                          Nov 25 '18 at 15:34

















                                        the word to replace can appears multiple times and I only want to replace one

                                        – Amadou Beye
                                        Nov 25 '18 at 15:34





                                        the word to replace can appears multiple times and I only want to replace one

                                        – Amadou Beye
                                        Nov 25 '18 at 15:34


















                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53468915%2freplace-part-of-string-at-specified-position%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Contact image not getting when fetch all contact list from iPhone by CNContact

                                        count number of partitions of a set with n elements into k subsets

                                        A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks