Remove character at a specific index from a string in javascript

We often encounter a widespread requirement to remove a character from a particular index in a javascript string. This article will illustrate how to remove a character from a specific index position in a string using different methods and examples.

Table of Contents:-

Javascript string remove character at a specific index using substring()

The substring() method in javascript returns a string segment between start and end indexes or to the end of the string.

Example:-

Remove the character ‘*’ at 17th position from string “Javascript- the *versatile language” based on index starting from 1.

Here, we will use the substring() method for dividing the original string into two substrings. The particular character (*) at a specific index gets excluded from both the substrings. Finally, join them back into the final string using the javascript concatenation (+) operator.

Code:-

let dummyString = "Javascript- the *versatile language"
let indexPosition = 17
let finalString = dummyString.substring(0, indexPosition - 1) + dummyString.substring(indexPosition, dummyString.length);
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Output:-

Original String: Javascript- the *versatile language
Final String: Javascript- the versatile language

Javascript string remove character at a specific index using slice()

Javascript’s slice() returns a copy of a part of a string as a new string without modifying the original string.

Example:-

Remove the character ‘*’ at 17th position from string “Javascript- the *versatile language” based on index starting from 1.

Similar to the substring() method slice() method is used in the below example code to divide the original string into two parts: two substrings such that the particular character at a specific index position gets omitted. Finally, join the substrings back to a string using the javascript concatenation (+) operator.

Code:-

let dummyString = "Javascript- the *versatile language"
let indexPosition = 17
finalString = dummyString.slice(0, indexPosition-1) + dummyString.slice(indexPosition) 
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Output:-

Original String: Javascript- the *versatile language
Final String: Javascript- the versatile language

Javascript string remove character at a specific index using split() and join()

The split() method in javascript returns an array of substrings formed by splitting a given string.

The join() method in javascript joins the elements of the array back into a string.

Example:-

Remove the character ‘*’ at 17th position from string “Javascript- the *versatile language” based on index starting from 1.

Here, in the below code, we split the original string into two substrings using the split() method by the character we need to remove- ‘*’. Then the two substrings, one before the character ‘*’ and another after the character ‘*’ are joined back using the join() method.

Code:-

let dummyString = "Javascript- the *versatile language"
let indexPosition = 17
let characterAtIndex = dummyString.charAt(indexPosition-1)
let finalString =  dummyString.split(characterAtIndex).join('')
console.log("Original String: "+ dummyString )
console.log("Final String: "+ finalString)

Output:-

Original String: Javascript- the *versatile language
Final String: Javascript- the versatile language

Read More:

I hope this article helped you to delete a character from a specific index in a javascript string. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top