5

I would like to remove the characters destructively from two points in a string, so when the string is called after the removal it would not include the removed characters.

Example

var string = "I am a string";

I'd like to: remove (0, 7);

When I call string again it should return:

console.log(string) => string

Example-2

var string = "I am a string";

I'd like to: remove (7, 10);

When I call string again it should return:

console.log(string) => I am a ing

Community
  • 1
  • 1
halfacreyum
  • 297
  • 5
  • 16
  • 1
    Strings are immutable, you have to build a new string from the bits you want to keep, you can't just remove the bits you don't want (i.e. there's no equivalent for Array's *splice*). – RobG Nov 21 '16 at 00:49
  • Great point! I tried to remove 'destructively' from the title but I couldn't. – halfacreyum Nov 21 '16 at 01:08

2 Answers2

5

See javascript substring.

For your example use this:

var string = "I am a string";
console.log(string.substring(7));

OUTPUTS

string

UPDATE

For removing a portionof a string, you can do it by concating the first wanted characters with the last wanted characters, something like this:

var string = "I am a string";
console.log(string.substr(0, 5) + string.substr(7));

OUTPUTS

I am string

If you want to have a direct function for removing portions of strings, see Ken White's answer that uses substr instead of substring. The difference between substr and substring is in the second parameter, for substring is the index to stop and for substr the length to return. You can use something like this:

String.prototype.replaceAt = function(index, charcount) {
  return this.substr(0, index) + this.substr(index + charcount);
}

string.replaceAt(5, 2); // Outputs: "I am string"

Or if you want to use start and end like (7, 10), then have a function like this:

String.prototype.removeAt = function(start, end) {
  return this.substr(0, start) + this.substr(end);
}

string.removeAt(7, 10); // Outputs: "I am a ing"
Community
  • 1
  • 1
Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
  • Thanks for the response, I should of clarified that I'd also like to remove characters from the middle of a string. Post updated. – halfacreyum Nov 21 '16 at 00:32
  • Note that [*substring*](http://ecma-international.org/ecma-262/7.0/index.html#sec-string.prototype.substring) and [*substr*](http://ecma-international.org/ecma-262/7.0/index.html#sec-string.prototype.substr) are subtly different. The first uses *start, end* parameters, the second *start, length*. – RobG Nov 21 '16 at 00:50
  • Great! Exactly what I was looking for, I'm surprised there isn't a method that does this. – halfacreyum Nov 21 '16 at 00:50
  • Just one problem, as RobG pointed in comment... console.log(string), after 'removal', returns original variable. – sinisake Nov 21 '16 at 00:53
  • @RobG yes I know. I got the `String.prototype.replaceAt` exactly as it is on Ken White's answer. I think it works quite well for such functionality, but I guess using `substring` here would be better since we have to deal with string indexes and not length. – Christos Lytras Nov 21 '16 at 00:55
  • Yes, that's true, but then you can assign the value to the string variable like this `string = string.removeAt(7, 10);`. It's pretty simple. I can update my answer if a so basic functionality needs clarification. We have the same functionality like `substring` or `substr` would work like, which both don't affect the string variable are called from, but return the result. – Christos Lytras Nov 21 '16 at 01:01
  • I was really just pointing out that the answer starts with a reference to *substring* and uses it in the first bit of code, but uses *substr* thereafter without making a note of it. – RobG Nov 21 '16 at 02:03
1

The easiest way is to just slice the front part and the back part and splice them back together:

var string = "I am a string";
string = string.substring(0, 7) + string.substring(10);
console.log(string);
// => I am a ing
Amadan
  • 191,408
  • 23
  • 240
  • 301