[javascript] Remove a character at a certain position in a string - javascript

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.

This question is related to javascript string

The answer is


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'.

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"

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(postion, str.length);
alert(newStr);

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


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.


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

you can use substring() method. ex,

var x = "Hello world"
var x = x.substring(0, i) + 'h' + x.substring(i+1);

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