Remove characters from string and return new string with removed characters





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







3















I'm trying to remove a few characters from a string and return the removed characters. Here's my code...






function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}

alert(removeFromString ('Hello', 0, 3)) //lo





I figured out how to return the remaining characters but I can't figure out how to return the characters that were removed.



Thanks in advance!










share|improve this question























  • you never remove characters from a string, because strings are immutable. you could select the other characters and return them.

    – Nina Scholz
    Nov 24 '18 at 16:57













  • My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.

    – Marsden Mars
    Nov 24 '18 at 16:58











  • Right now it returns ('Hel'), I'm trying to get it to return ('lo')

    – Marsden Mars
    Nov 24 '18 at 16:59


















3















I'm trying to remove a few characters from a string and return the removed characters. Here's my code...






function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}

alert(removeFromString ('Hello', 0, 3)) //lo





I figured out how to return the remaining characters but I can't figure out how to return the characters that were removed.



Thanks in advance!










share|improve this question























  • you never remove characters from a string, because strings are immutable. you could select the other characters and return them.

    – Nina Scholz
    Nov 24 '18 at 16:57













  • My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.

    – Marsden Mars
    Nov 24 '18 at 16:58











  • Right now it returns ('Hel'), I'm trying to get it to return ('lo')

    – Marsden Mars
    Nov 24 '18 at 16:59














3












3








3


0






I'm trying to remove a few characters from a string and return the removed characters. Here's my code...






function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}

alert(removeFromString ('Hello', 0, 3)) //lo





I figured out how to return the remaining characters but I can't figure out how to return the characters that were removed.



Thanks in advance!










share|improve this question














I'm trying to remove a few characters from a string and return the removed characters. Here's my code...






function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}

alert(removeFromString ('Hello', 0, 3)) //lo





I figured out how to return the remaining characters but I can't figure out how to return the characters that were removed.



Thanks in advance!






function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}

alert(removeFromString ('Hello', 0, 3)) //lo





function removeFromString(string, start, charToRemove){
var newString = ''
newString = string.slice(start, charToRemove);
return newString
}

alert(removeFromString ('Hello', 0, 3)) //lo






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 16:56









Marsden MarsMarsden Mars

254




254













  • you never remove characters from a string, because strings are immutable. you could select the other characters and return them.

    – Nina Scholz
    Nov 24 '18 at 16:57













  • My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.

    – Marsden Mars
    Nov 24 '18 at 16:58











  • Right now it returns ('Hel'), I'm trying to get it to return ('lo')

    – Marsden Mars
    Nov 24 '18 at 16:59



















  • you never remove characters from a string, because strings are immutable. you could select the other characters and return them.

    – Nina Scholz
    Nov 24 '18 at 16:57













  • My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.

    – Marsden Mars
    Nov 24 '18 at 16:58











  • Right now it returns ('Hel'), I'm trying to get it to return ('lo')

    – Marsden Mars
    Nov 24 '18 at 16:59

















you never remove characters from a string, because strings are immutable. you could select the other characters and return them.

– Nina Scholz
Nov 24 '18 at 16:57







you never remove characters from a string, because strings are immutable. you could select the other characters and return them.

– Nina Scholz
Nov 24 '18 at 16:57















My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.

– Marsden Mars
Nov 24 '18 at 16:58





My apologies, I'm not trying to mutate the string, I'm trying to put the characters in a new string and return that.

– Marsden Mars
Nov 24 '18 at 16:58













Right now it returns ('Hel'), I'm trying to get it to return ('lo')

– Marsden Mars
Nov 24 '18 at 16:59





Right now it returns ('Hel'), I'm trying to get it to return ('lo')

– Marsden Mars
Nov 24 '18 at 16:59












7 Answers
7






active

oldest

votes


















2














I modified your own code to return the remaining characters:






function removeFromString(string, start, charToRemove){
var newString = '';
newString = string.slice(0, start) + string.slice(start+charToRemove);
return newString;
}

console.log(removeFromString ('Hello', 0, 3)) //lo








share|improve this answer































    2














    You can convert the string to an array, use splice method to split the unused items, then joining the result items in the array to a string.






    var arr = Array.from('Hello');

    var items = arr.splice(3, arr.length);

    var result = items.join('');

    console.log(result);








    share|improve this answer
























    • This solution is not complete. It assumes the removal always starts at 0.

      – Alimo
      Nov 25 '18 at 13:25



















    2














    You could map the characters and remove the caracters who are in the given range.






    function removeFromString(string, start, charToRemove) {
    return Array.from(
    string,
    (c, i) => i >= start && i < start + charToRemove ? '': c
    ).join('');
    }

    console.log(removeFromString ('Hello', 0, 3)) //lo








    share|improve this answer































      1














      string.slice(start, charToRemove) will return a new string value, which is created by taking all the characters from the string value inside the variable string from index start to index charToRemove



      With your example call now, the string value inside the string variable is:



      H e l l o
      0 1 2 3 4


      and the start index you give is 0 and charToRemove is 3, so we get a new string value back that contains the characters at index 0, 1 and 2 which is H, e and l.



      If you want everything starting from index 3 you need to give 3 as a offset to the slice method, i.e.



      'Hello'.slice(3)



      I'm not entirely sure what exactly your goal is, though. If you want to get the last n characters in a string you can give a negative offset like string.slice(-2).






      share|improve this answer































        1














        You could use substring to get the part that you want to remove and replace it with empty string.
        This way we don't need loop, or converting the string to array.






        function removeFromString(str, start, charToRemove){
        let end = start+charToRemove
        let strToBeRemoved = str.substring(start, end)
        return str.replace(strToBeRemoved, "")
        }


        console.log(removeFromString ('Hello', 0, 3)) //lo
        console.log(removeFromString ('Hello', 2, 2)) //Heo
        console.log(removeFromString ('Hello', 1, 3)) //Ho








        share|improve this answer

































          0














          Just make your function return this : string.substr(0,start)+string.substr(charToRemove+1)






          share|improve this answer































            0














            Here's a "multislice" function that cuts the string at specific positions and returns an array of slices. For example, segment(hello, 0, 3) returns [hel, lo].






            function segment(str, ...positions) {
            let last = 0, res =

            for (let pos of positions) {
            res.push(str.slice(last, pos))
            last = pos
            }

            if (last < str.length)
            res.push(str.slice(last))

            return res

            }

            console.log(segment('Hello', 0, 3))








            share|improve this answer
























              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%2f53460413%2fremove-characters-from-string-and-return-new-string-with-removed-characters%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              7 Answers
              7






              active

              oldest

              votes








              7 Answers
              7






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              I modified your own code to return the remaining characters:






              function removeFromString(string, start, charToRemove){
              var newString = '';
              newString = string.slice(0, start) + string.slice(start+charToRemove);
              return newString;
              }

              console.log(removeFromString ('Hello', 0, 3)) //lo








              share|improve this answer




























                2














                I modified your own code to return the remaining characters:






                function removeFromString(string, start, charToRemove){
                var newString = '';
                newString = string.slice(0, start) + string.slice(start+charToRemove);
                return newString;
                }

                console.log(removeFromString ('Hello', 0, 3)) //lo








                share|improve this answer


























                  2












                  2








                  2







                  I modified your own code to return the remaining characters:






                  function removeFromString(string, start, charToRemove){
                  var newString = '';
                  newString = string.slice(0, start) + string.slice(start+charToRemove);
                  return newString;
                  }

                  console.log(removeFromString ('Hello', 0, 3)) //lo








                  share|improve this answer













                  I modified your own code to return the remaining characters:






                  function removeFromString(string, start, charToRemove){
                  var newString = '';
                  newString = string.slice(0, start) + string.slice(start+charToRemove);
                  return newString;
                  }

                  console.log(removeFromString ('Hello', 0, 3)) //lo








                  function removeFromString(string, start, charToRemove){
                  var newString = '';
                  newString = string.slice(0, start) + string.slice(start+charToRemove);
                  return newString;
                  }

                  console.log(removeFromString ('Hello', 0, 3)) //lo





                  function removeFromString(string, start, charToRemove){
                  var newString = '';
                  newString = string.slice(0, start) + string.slice(start+charToRemove);
                  return newString;
                  }

                  console.log(removeFromString ('Hello', 0, 3)) //lo






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 17:07









                  AlimoAlimo

                  914




                  914

























                      2














                      You can convert the string to an array, use splice method to split the unused items, then joining the result items in the array to a string.






                      var arr = Array.from('Hello');

                      var items = arr.splice(3, arr.length);

                      var result = items.join('');

                      console.log(result);








                      share|improve this answer
























                      • This solution is not complete. It assumes the removal always starts at 0.

                        – Alimo
                        Nov 25 '18 at 13:25
















                      2














                      You can convert the string to an array, use splice method to split the unused items, then joining the result items in the array to a string.






                      var arr = Array.from('Hello');

                      var items = arr.splice(3, arr.length);

                      var result = items.join('');

                      console.log(result);








                      share|improve this answer
























                      • This solution is not complete. It assumes the removal always starts at 0.

                        – Alimo
                        Nov 25 '18 at 13:25














                      2












                      2








                      2







                      You can convert the string to an array, use splice method to split the unused items, then joining the result items in the array to a string.






                      var arr = Array.from('Hello');

                      var items = arr.splice(3, arr.length);

                      var result = items.join('');

                      console.log(result);








                      share|improve this answer













                      You can convert the string to an array, use splice method to split the unused items, then joining the result items in the array to a string.






                      var arr = Array.from('Hello');

                      var items = arr.splice(3, arr.length);

                      var result = items.join('');

                      console.log(result);








                      var arr = Array.from('Hello');

                      var items = arr.splice(3, arr.length);

                      var result = items.join('');

                      console.log(result);





                      var arr = Array.from('Hello');

                      var items = arr.splice(3, arr.length);

                      var result = items.join('');

                      console.log(result);






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 24 '18 at 17:01









                      FooFoo

                      1




                      1













                      • This solution is not complete. It assumes the removal always starts at 0.

                        – Alimo
                        Nov 25 '18 at 13:25



















                      • This solution is not complete. It assumes the removal always starts at 0.

                        – Alimo
                        Nov 25 '18 at 13:25

















                      This solution is not complete. It assumes the removal always starts at 0.

                      – Alimo
                      Nov 25 '18 at 13:25





                      This solution is not complete. It assumes the removal always starts at 0.

                      – Alimo
                      Nov 25 '18 at 13:25











                      2














                      You could map the characters and remove the caracters who are in the given range.






                      function removeFromString(string, start, charToRemove) {
                      return Array.from(
                      string,
                      (c, i) => i >= start && i < start + charToRemove ? '': c
                      ).join('');
                      }

                      console.log(removeFromString ('Hello', 0, 3)) //lo








                      share|improve this answer




























                        2














                        You could map the characters and remove the caracters who are in the given range.






                        function removeFromString(string, start, charToRemove) {
                        return Array.from(
                        string,
                        (c, i) => i >= start && i < start + charToRemove ? '': c
                        ).join('');
                        }

                        console.log(removeFromString ('Hello', 0, 3)) //lo








                        share|improve this answer


























                          2












                          2








                          2







                          You could map the characters and remove the caracters who are in the given range.






                          function removeFromString(string, start, charToRemove) {
                          return Array.from(
                          string,
                          (c, i) => i >= start && i < start + charToRemove ? '': c
                          ).join('');
                          }

                          console.log(removeFromString ('Hello', 0, 3)) //lo








                          share|improve this answer













                          You could map the characters and remove the caracters who are in the given range.






                          function removeFromString(string, start, charToRemove) {
                          return Array.from(
                          string,
                          (c, i) => i >= start && i < start + charToRemove ? '': c
                          ).join('');
                          }

                          console.log(removeFromString ('Hello', 0, 3)) //lo








                          function removeFromString(string, start, charToRemove) {
                          return Array.from(
                          string,
                          (c, i) => i >= start && i < start + charToRemove ? '': c
                          ).join('');
                          }

                          console.log(removeFromString ('Hello', 0, 3)) //lo





                          function removeFromString(string, start, charToRemove) {
                          return Array.from(
                          string,
                          (c, i) => i >= start && i < start + charToRemove ? '': c
                          ).join('');
                          }

                          console.log(removeFromString ('Hello', 0, 3)) //lo






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 24 '18 at 17:02









                          Nina ScholzNina Scholz

                          199k15112182




                          199k15112182























                              1














                              string.slice(start, charToRemove) will return a new string value, which is created by taking all the characters from the string value inside the variable string from index start to index charToRemove



                              With your example call now, the string value inside the string variable is:



                              H e l l o
                              0 1 2 3 4


                              and the start index you give is 0 and charToRemove is 3, so we get a new string value back that contains the characters at index 0, 1 and 2 which is H, e and l.



                              If you want everything starting from index 3 you need to give 3 as a offset to the slice method, i.e.



                              'Hello'.slice(3)



                              I'm not entirely sure what exactly your goal is, though. If you want to get the last n characters in a string you can give a negative offset like string.slice(-2).






                              share|improve this answer




























                                1














                                string.slice(start, charToRemove) will return a new string value, which is created by taking all the characters from the string value inside the variable string from index start to index charToRemove



                                With your example call now, the string value inside the string variable is:



                                H e l l o
                                0 1 2 3 4


                                and the start index you give is 0 and charToRemove is 3, so we get a new string value back that contains the characters at index 0, 1 and 2 which is H, e and l.



                                If you want everything starting from index 3 you need to give 3 as a offset to the slice method, i.e.



                                'Hello'.slice(3)



                                I'm not entirely sure what exactly your goal is, though. If you want to get the last n characters in a string you can give a negative offset like string.slice(-2).






                                share|improve this answer


























                                  1












                                  1








                                  1







                                  string.slice(start, charToRemove) will return a new string value, which is created by taking all the characters from the string value inside the variable string from index start to index charToRemove



                                  With your example call now, the string value inside the string variable is:



                                  H e l l o
                                  0 1 2 3 4


                                  and the start index you give is 0 and charToRemove is 3, so we get a new string value back that contains the characters at index 0, 1 and 2 which is H, e and l.



                                  If you want everything starting from index 3 you need to give 3 as a offset to the slice method, i.e.



                                  'Hello'.slice(3)



                                  I'm not entirely sure what exactly your goal is, though. If you want to get the last n characters in a string you can give a negative offset like string.slice(-2).






                                  share|improve this answer













                                  string.slice(start, charToRemove) will return a new string value, which is created by taking all the characters from the string value inside the variable string from index start to index charToRemove



                                  With your example call now, the string value inside the string variable is:



                                  H e l l o
                                  0 1 2 3 4


                                  and the start index you give is 0 and charToRemove is 3, so we get a new string value back that contains the characters at index 0, 1 and 2 which is H, e and l.



                                  If you want everything starting from index 3 you need to give 3 as a offset to the slice method, i.e.



                                  'Hello'.slice(3)



                                  I'm not entirely sure what exactly your goal is, though. If you want to get the last n characters in a string you can give a negative offset like string.slice(-2).







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Nov 24 '18 at 17:09









                                  StefanStefan

                                  60028




                                  60028























                                      1














                                      You could use substring to get the part that you want to remove and replace it with empty string.
                                      This way we don't need loop, or converting the string to array.






                                      function removeFromString(str, start, charToRemove){
                                      let end = start+charToRemove
                                      let strToBeRemoved = str.substring(start, end)
                                      return str.replace(strToBeRemoved, "")
                                      }


                                      console.log(removeFromString ('Hello', 0, 3)) //lo
                                      console.log(removeFromString ('Hello', 2, 2)) //Heo
                                      console.log(removeFromString ('Hello', 1, 3)) //Ho








                                      share|improve this answer






























                                        1














                                        You could use substring to get the part that you want to remove and replace it with empty string.
                                        This way we don't need loop, or converting the string to array.






                                        function removeFromString(str, start, charToRemove){
                                        let end = start+charToRemove
                                        let strToBeRemoved = str.substring(start, end)
                                        return str.replace(strToBeRemoved, "")
                                        }


                                        console.log(removeFromString ('Hello', 0, 3)) //lo
                                        console.log(removeFromString ('Hello', 2, 2)) //Heo
                                        console.log(removeFromString ('Hello', 1, 3)) //Ho








                                        share|improve this answer




























                                          1












                                          1








                                          1







                                          You could use substring to get the part that you want to remove and replace it with empty string.
                                          This way we don't need loop, or converting the string to array.






                                          function removeFromString(str, start, charToRemove){
                                          let end = start+charToRemove
                                          let strToBeRemoved = str.substring(start, end)
                                          return str.replace(strToBeRemoved, "")
                                          }


                                          console.log(removeFromString ('Hello', 0, 3)) //lo
                                          console.log(removeFromString ('Hello', 2, 2)) //Heo
                                          console.log(removeFromString ('Hello', 1, 3)) //Ho








                                          share|improve this answer















                                          You could use substring to get the part that you want to remove and replace it with empty string.
                                          This way we don't need loop, or converting the string to array.






                                          function removeFromString(str, start, charToRemove){
                                          let end = start+charToRemove
                                          let strToBeRemoved = str.substring(start, end)
                                          return str.replace(strToBeRemoved, "")
                                          }


                                          console.log(removeFromString ('Hello', 0, 3)) //lo
                                          console.log(removeFromString ('Hello', 2, 2)) //Heo
                                          console.log(removeFromString ('Hello', 1, 3)) //Ho








                                          function removeFromString(str, start, charToRemove){
                                          let end = start+charToRemove
                                          let strToBeRemoved = str.substring(start, end)
                                          return str.replace(strToBeRemoved, "")
                                          }


                                          console.log(removeFromString ('Hello', 0, 3)) //lo
                                          console.log(removeFromString ('Hello', 2, 2)) //Heo
                                          console.log(removeFromString ('Hello', 1, 3)) //Ho





                                          function removeFromString(str, start, charToRemove){
                                          let end = start+charToRemove
                                          let strToBeRemoved = str.substring(start, end)
                                          return str.replace(strToBeRemoved, "")
                                          }


                                          console.log(removeFromString ('Hello', 0, 3)) //lo
                                          console.log(removeFromString ('Hello', 2, 2)) //Heo
                                          console.log(removeFromString ('Hello', 1, 3)) //Ho






                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Nov 24 '18 at 17:30

























                                          answered Nov 24 '18 at 17:10









                                          Christhofer NataliusChristhofer Natalius

                                          855923




                                          855923























                                              0














                                              Just make your function return this : string.substr(0,start)+string.substr(charToRemove+1)






                                              share|improve this answer




























                                                0














                                                Just make your function return this : string.substr(0,start)+string.substr(charToRemove+1)






                                                share|improve this answer


























                                                  0












                                                  0








                                                  0







                                                  Just make your function return this : string.substr(0,start)+string.substr(charToRemove+1)






                                                  share|improve this answer













                                                  Just make your function return this : string.substr(0,start)+string.substr(charToRemove+1)







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 24 '18 at 17:15









                                                  KingkostiaKingkostia

                                                  1




                                                  1























                                                      0














                                                      Here's a "multislice" function that cuts the string at specific positions and returns an array of slices. For example, segment(hello, 0, 3) returns [hel, lo].






                                                      function segment(str, ...positions) {
                                                      let last = 0, res =

                                                      for (let pos of positions) {
                                                      res.push(str.slice(last, pos))
                                                      last = pos
                                                      }

                                                      if (last < str.length)
                                                      res.push(str.slice(last))

                                                      return res

                                                      }

                                                      console.log(segment('Hello', 0, 3))








                                                      share|improve this answer




























                                                        0














                                                        Here's a "multislice" function that cuts the string at specific positions and returns an array of slices. For example, segment(hello, 0, 3) returns [hel, lo].






                                                        function segment(str, ...positions) {
                                                        let last = 0, res =

                                                        for (let pos of positions) {
                                                        res.push(str.slice(last, pos))
                                                        last = pos
                                                        }

                                                        if (last < str.length)
                                                        res.push(str.slice(last))

                                                        return res

                                                        }

                                                        console.log(segment('Hello', 0, 3))








                                                        share|improve this answer


























                                                          0












                                                          0








                                                          0







                                                          Here's a "multislice" function that cuts the string at specific positions and returns an array of slices. For example, segment(hello, 0, 3) returns [hel, lo].






                                                          function segment(str, ...positions) {
                                                          let last = 0, res =

                                                          for (let pos of positions) {
                                                          res.push(str.slice(last, pos))
                                                          last = pos
                                                          }

                                                          if (last < str.length)
                                                          res.push(str.slice(last))

                                                          return res

                                                          }

                                                          console.log(segment('Hello', 0, 3))








                                                          share|improve this answer













                                                          Here's a "multislice" function that cuts the string at specific positions and returns an array of slices. For example, segment(hello, 0, 3) returns [hel, lo].






                                                          function segment(str, ...positions) {
                                                          let last = 0, res =

                                                          for (let pos of positions) {
                                                          res.push(str.slice(last, pos))
                                                          last = pos
                                                          }

                                                          if (last < str.length)
                                                          res.push(str.slice(last))

                                                          return res

                                                          }

                                                          console.log(segment('Hello', 0, 3))








                                                          function segment(str, ...positions) {
                                                          let last = 0, res =

                                                          for (let pos of positions) {
                                                          res.push(str.slice(last, pos))
                                                          last = pos
                                                          }

                                                          if (last < str.length)
                                                          res.push(str.slice(last))

                                                          return res

                                                          }

                                                          console.log(segment('Hello', 0, 3))





                                                          function segment(str, ...positions) {
                                                          let last = 0, res =

                                                          for (let pos of positions) {
                                                          res.push(str.slice(last, pos))
                                                          last = pos
                                                          }

                                                          if (last < str.length)
                                                          res.push(str.slice(last))

                                                          return res

                                                          }

                                                          console.log(segment('Hello', 0, 3))






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Nov 24 '18 at 17:34









                                                          georggeorg

                                                          155k35209307




                                                          155k35209307






























                                                              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%2f53460413%2fremove-characters-from-string-and-return-new-string-with-removed-characters%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







                                                              這個網誌中的熱門文章

                                                              Academy of Television Arts & Sciences

                                                              L'Équipe

                                                              1995 France bombings