1

I wish to use splice on string e.g.

console.log(removeFromString('Elie', 2, 2)) // 'El'
12
  • 1
    Please read this: How much research effort is expected of Stack Overflow users? Commented Sep 30, 2021 at 7:24
  • Start with the documentation. String.prototype.splice won’t exist, because strings are immutable. Use slice or substring instead. Commented Sep 30, 2021 at 7:24
  • 2
    @AbinThaha Please stop editing the question to fit your interpretation. And if you edit, at least don't break the formatting. Commented Sep 30, 2021 at 7:28
  • 1
    @AmrendraK If you know that splice can be used with arrays, why not make a character array, then splice, then convert back to a string? Again, start with the documentation, e.g. the Array methods. Try something on your own. All the tools you’ll need are documented there. Commented Sep 30, 2021 at 7:28
  • 1
    @AmrendraK And what does splice do to the string? Can you at least try to describe the algorithm? Perhaps in terms of the method names provided in the documentation links? Perhaps even in the form of code that you tried? Commented Sep 30, 2021 at 7:34

5 Answers 5

3

If you really want to use splice(), you can spread the string to an array, invoke splice() on the array, and join() the results back together:

const removeFromString = (s, x, y) => {
  const array = [...s];
  array.splice(x, y);
  return array.join('');
}

console.log(removeFromString('Elie', 2, 2));

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

Comments

2

Although this is not a great way but this is just to give you idea how things works in javascript.

  1. You can't use splice on a string because it works for arrays as it doesn’t work on strings because strings are immutable (edit after
    Sebastian Simon's comment)

  2. You can use split method to convert a string into array of characters.

  3. You can use splice method on array of characters.

  4. You can use join method to convert array of characters to make a string

let stringTitle = "Elie";   // string
let stringArray = stringTitle.split("");   // spliting string into array of characters
console.log(stringArray);
let subString = stringArray.splice(0,2).join("")  // using splice on array of characters and then join them to make a string
console.log(subString)  // output El

I would suggest using str.substring(0, 2); for this use case is a better option.

let stringExample = "Elie";
let substring = stringExample.substring(0, 2);
console.log(substring) // output El

3 Comments

“You can't use splice on a string because it works for arrays.” — This reasoning is not quite correct, or at least not complete. Mainly, it doesn’t work on strings because strings are immutable. You can totally attempt to do Array.prototype.splice.call("abc", 1, 0), but you’ll get an error whose cause is the string’s immutability. Also, the splitsplicejoin approach has already been suggested, and my comment on the other answer still holds: .split is not Unicode-aware. Please use Array.from instead.
Thanks.. let me add this statement in the answer.
I already have mentioned this is just to give an idea. It is not a great approach. And, I didn't read your comment on the answer before posting the answer.
1

I think this is what you want to do.

function removeFromString(string, start, count) {
    let str = string.split('');
    str.splice(start, count);
    return str.join('');
}

console.log(removeFromString('Elie', 2, 2));

2 Comments

.split('') is not Unicode-aware. Please use Array.from to support a larger variety of characters.
@SebastianSimon Yes good approach. Thank you!
1

Try this:

function removeFromString(str, start, end) {
  let arr = Array.from(str);
  arr.splice(start, end);
  return arr.join(String());
}

and then use:

removeFromString('Elie', 2, 2);

3 Comments

String() really isn’t best practice. Use "" instead.
"" - is a hardcode
Uh, yes, so is String(). That’s what you want in .join.
0

Try using following code you can get more info about slice here

let str = "Elie";
// pass start and end position to slice method ,it will not include end character like i is at 2nd position
console.log(str.slice(0, 2));

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.