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?

3
  • String.prototype.slice: "Return value - A new string containing the extracted section of the string." Commented Dec 28, 2019 at 16:17
  • Is your expected outcome "This string" for removeFromString('This is a string', 5, 5)? Commented Dec 28, 2019 at 16:25
  • @AjayDabas exactly, it would start at the 5th index and then remove the next 5 characters. Commented Dec 28, 2019 at 16:26

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Nice, but be careful about splitting on an empty string since this won't always give the expected results. (See: stackoverflow.com/questions/4547609/…).
yeah thank you so much, we can use Array.from(string) or [...string] To avoid this case
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)
);

Comments

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

1 Comment

Note that splitting on an empty string isn't a best practice -- but filling with an empty string is a fun trick!
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;
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.