2

I am trying to remove n amount of characters from a given index. Here are the instructions:

"Write a function called removeFromString, which accepts a string, a starting index (number) and a number of characters to remove.

The function should return a new string with the characters removed."

Here is what I have so far:

function removeFromString(str, index, number) {
  var sliced = str.slice(index, -number);
  return sliced;
}

console.log(
  removeFromString('This is a string', 5, 5)
  );

It is sort of working, BUT for some reason in addition to removing characters from the given index, it also removes characters from the end of the string. What am I doing wrong?

  • `String.prototype.slice`: ["Return value - A new string containing the extracted section of the string."](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice#Return_value) – ASDFGerte Dec 28 '19 at 16:17
  • Is your expected outcome `"This string"` for `removeFromString('This is a string', 5, 5)`? – Ajay Dabas Dec 28 '19 at 16:25
  • @AjayDabas exactly, it would start at the 5th index and then remove the next 5 characters. –  Dec 28 '19 at 16:26

5 Answers5

3

function removeFromString(str, index, number) {
  var outputStringArray = str.split('');
  outputStringArray.splice(index, number);
  return outputStringArray.join('');
}

console.log(
  removeFromString('This is a string', 5, 5)
  );
Jagadeesh
  • 52
  • 4
  • Nice, but be careful about splitting on an empty string since this won't always give the expected results. (See: https://stackoverflow.com/questions/4547609/how-do-you-get-a-string-to-a-character-array-in-javascript). – Cat Jan 06 '20 at 12:38
  • yeah thank you so much, we can use Array.from(string) or [...string] To avoid this case – Jagadeesh Jan 12 '20 at 17:37
2

Slice returns the extracted string... so, put two extracted strings together.

function removeFromString(str, index, number) {
  var sliced = str.slice(0, index) + str.slice(index + number);
  return sliced;
}

console.log(
  removeFromString('This is a string', 5, 5)
);
Yousername
  • 1,012
  • 5
  • 15
1

For the Nerds

function removeFromString(str,index,number){
  return str.split('').fill('',index,index+number).join('')
}
console.log(removeFromString('This is a string',5,5))
fubar
  • 383
  • 2
  • 11
  • 1
    Note that splitting on an empty string isn't a best practice -- but filling with an empty string is a fun trick! – Cat Jan 06 '20 at 12:40
0

If you convert the String to an Array, you can use the powerful array.splice method. Here's an intentionally verbose example:

let str = "Harry Potter-Evans-Verres and the Methods of Rationality"
removeFromString(str, 12, 13);

function removeFromString(string, startAt, howMany){

  const array = Array.from(string)
  removedArray = array.splice(12, 13);
  let remainingString = array.join("");

  let removedString = removedArray.join("");
  console.log({remainingString});
  console.log({removedString});

  return remainingString;
}
Cat
  • 4,141
  • 2
  • 10
  • 18
0

This should work:

function removeFromString(str, start, removeCount) {
  let newStr = '';

  for (let i = 0; i < str.length; i++) {

    if (i < start || i >= start + removeCount) {

      newStr += str[i];
    }
  }
  return newStr;
}

let test = removeFromString('this is a test', 4, 6);
console.log(test);
Lewis
  • 4,285
  • 1
  • 23
  • 36