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

JavascriptString

Javascript Problem Overview


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 https://stackoverflow.com/questions/9932957, because this one is about removing the character at a specific position, and that question is about removing all instances of a character.

Javascript Solutions


Solution 1 - Javascript

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)

Solution 2 - Javascript

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

Solution 3 - Javascript

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

Solution 4 - Javascript

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"

Solution 5 - Javascript

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

Solution 6 - Javascript

you can use substring() method. ex,

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

Solution 7 - Javascript

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

Solution 8 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstarbeamrainbowlabsView Question on Stackoverflow
Solution 1 - JavascriptMattView Answer on Stackoverflow
Solution 2 - JavascriptIshan DhingraView Answer on Stackoverflow
Solution 3 - JavascriptAlexandre DaubricourtView Answer on Stackoverflow
Solution 4 - JavascriptSurendranath SonawaneView Answer on Stackoverflow
Solution 5 - JavascriptNikhil DView Answer on Stackoverflow
Solution 6 - Javascriptuser1043086View Answer on Stackoverflow
Solution 7 - JavascriptRamThakurView Answer on Stackoverflow
Solution 8 - JavascriptartisView Answer on Stackoverflow