admin管理员组

文章数量:1398216

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'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 asked Nov 24, 2018 at 16:56 Marsden MarsMarsden Mars 451 silver badge6 bronze badges 3
  • you never remove characters from a string, because strings are immutable. you could select the other characters and return them. – Nina Scholz Commented Nov 24, 2018 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 Commented Nov 24, 2018 at 16:58
  • Right now it returns ('Hel'), I'm trying to get it to return ('lo') – Marsden Mars Commented Nov 24, 2018 at 16:59
Add a ment  | 

7 Answers 7

Reset to default 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

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);

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

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).

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

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

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))

本文标签: javascriptRemove characters from string and return new string with removed charactersStack Overflow