Remove character from string javascript by index

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.

asked Jun 20, 2012 at 9:24

5

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,3254 gold badges22 silver badges37 bronze badges

answered Jun 20, 2012 at 9:26

MattMatt

73.1k26 gold badges149 silver badges179 bronze badges

4

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: //jsbin.com/ogagaq

zyrup

6412 gold badges9 silver badges16 bronze badges

answered Jun 20, 2012 at 9:33

Ishan DhingraIshan Dhingra

2,3845 gold badges30 silver badges42 bronze badges

2

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

answered Apr 10, 2017 at 11:37

2

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"

answered May 3, 2017 at 12:57

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

answered Jun 20, 2012 at 9:32

Nikhil DNikhil D

2,4363 gold badges20 silver badges41 bronze badges

0

you can use substring[] method. ex,

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

Bhavesh G

2,9904 gold badges38 silver badges66 bronze badges

answered Jun 20, 2012 at 9:46

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

answered Feb 4, 2016 at 10:52

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.4k24 gold badges95 silver badges149 bronze badges

answered Jun 20, 2012 at 9:33

artisartis

192 bronze badges

1

How do I remove a character from a string using index?

The str. replace[] can possibly be used for performing the task of removal as we can replace the particular index with empty char, and hence solve the issue.

How do I remove a character from a string in Javascript?

To remove a character from a string, use string replace[] and regular expression. This combination is used to remove all occurrences of the particular character, unlike the previous function. A regular expression is used instead of a string along with global property.

How do I remove a character from an index in a string in Java?

It has the method deleteCharAt[] , along with many other mutator methods. Just delete the characters that you need to delete and then get the result as follows: String resultString = sb. toString[];

How do I remove a specific character from a string?

Using 'str. replace[] , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace[] method will replace all occurrences of the specific character mentioned.

Chủ Đề