86

Is there an easy way to remove the character at a certain position in javascript?

e.g. if I have the string "Hello World", can I remove the character at position 3?

the result I would be looking for would the following:

"Helo World"

This question isn't a duplicate of How can I remove a character from a string using JavaScript?, because this one is about removing the character at a specific position, and that question is about removing all instances of a character.

starbeamrainbowlabs
  • 5,236
  • 7
  • 41
  • 71
  • Did you even look for an answer before posting this question? If you Goggled "Javascript string remove character" you would have come across at least 4 other questions from here, not to mention the actual solution about a million times. – phenomnomnominal Jun 20 '12 at 09:46
  • I did, but I can't have looked very hard – starbeamrainbowlabs Jun 21 '12 at 10:10
  • 17
    This isn't a duplicate. This question is about removing an known character from a certain position. The duplicate is about removing a known character from an arbitrary position. – Matt Sep 18 '14 at 09:04
  • 2
    @MT0 Nope. This question is about removing the character at a particular index - and that question is about removing instances of a particular character from a string. – starbeamrainbowlabs May 04 '17 at 13:01
  • @starbeamrainbowlabs No, it is an exact duplicate; the linked question is about removing the 4th character (which just happens to also always be an `r`). The [accepted answer here](http://stackoverflow.com/a/11116529/1509264) is identical to the [2nd highest voted answer](http://stackoverflow.com/a/9933072/1509264) on that question. – MT0 May 04 '17 at 13:33

8 Answers8

141

It depends how easy you find the following, which uses simple String methods (in this case slice()).

var str = "Hello World";
str = str.slice(0, 3) + str.slice(4);
console.log(str)
Jonathan
  • 2,225
  • 4
  • 20
  • 37
Matt
  • 72,564
  • 26
  • 147
  • 178
  • 1
    Thank You! I knew that there would be a simple solution. I didn't think of that :-) – starbeamrainbowlabs Jun 20 '12 at 09:29
  • @Zon: Exactly, and that's the idea. When coupled with `+ str.slice(4);`, you end up removing the 3rd character (zero indexed) in the string `str`. `str.slice(0, 3) + str.slice(4) // Helo World` – Matt Dec 09 '14 at 13:02
  • 1
    Just to put my dollar: str.slice(0, 3) gives 'Hel' (take from 1st to 3rd symbol); str.slice(4) gives 'o World' (take all after 4th symbol); you can also set cutting borders like this: str.slice(0, -6) to get 'Hello'. – Zon Dec 09 '14 at 13:06
  • function removeByIndex(aStr,index){ if(index==aStr.length-1){ return aStr.slice(0,aStr.length-1); } else{ return aStr.slice(0,index)+aStr.slice(index+1); } } function myFunction() { var str = "abc"; var res =removeByIndex(str,2); console.log(res); } – Asutosh Aug 09 '19 at 04:37
19

You can try it this way:

var str = "Hello World";
var position = 6; // its 1 based
var newStr = str.substring(0, position - 1) + str.substring(position, str.length);
alert(newStr);

Here is a live example: http://jsbin.com/ogagaq

zyrup
  • 611
  • 2
  • 9
  • 16
Ishan Dhingra
  • 2,376
  • 5
  • 30
  • 42
16

Turn the string into array, cut a character at specified index and turn back to string

let str = 'Hello World'.split('')

str.splice(3, 1)
str = str.join('')

// str = 'Helo World'.
Alexandre Daubricourt
  • 2,371
  • 1
  • 23
  • 27
5

If you omit the particular index character then use this method

function removeByIndex(str,index) {
      return str.slice(0,index) + str.slice(index+1);
}
    
var str = "Hello world", index=3;
console.log(removeByIndex(str,index));

// Output: "Helo world"
Surendranath Sonawane
  • 1,407
  • 1
  • 16
  • 23
4
    var str = 'Hello World';
                str = setCharAt(str, 3, '');
                alert(str);

function setCharAt(str, index, chr)
        {
            if (index > str.length - 1) return str;
            return str.substr(0, index) + chr + str.substr(index + 1);
        }
Nikhil D
  • 2,436
  • 3
  • 20
  • 41
1

you can use substring() method. ex,

var x = "Hello world"
var x = x.substring(0, i) + 'h' + x.substring(i+1);
Bhavesh G
  • 2,972
  • 4
  • 37
  • 65
0

Hi starbeamrainbowlabs ,

You can do this with the following:

var oldValue = "pic quality, hello" ;
var newValue =  "hello";
var oldValueLength = oldValue.length ;
var newValueLength = newValue.length ;
var from = oldValue.search(newValue) ;
var to = from + newValueLength ;
var nes = oldValue.substr(0,from) + oldValue.substr(to,oldValueLength);
console.log(nes);

I tested this in my javascript console so you can also check this out Thanks

starbeamrainbowlabs
  • 5,236
  • 7
  • 41
  • 71
RamThakur
  • 90
  • 9
-1
var str = 'Hello World',
    i = 3,
    result = str.substr(0, i-1)+str.substring(i);

alert(result);

Value of i should not be less then 1.

tereško
  • 57,247
  • 24
  • 95
  • 149
artis
  • 19
  • 2