How to use substr() method In Javascript

The substr() method is used to remove a character from a specific index within the string.

It extracts portions of a string between specified parameters and then joins the parts before and after the character to be removed.

Syntax:

string.substr(start, length)

Example: This example shows the above-explained approach

Javascript
function removeCharacter(position) {
    originalString = "w3wiki";
    newString =
        originalString.substr(0, position - 1)+
        originalString.substr(
            position,
            originalString.length
        );
    console.log(newString);
}

removeCharacter(6);

Output
GeeksorGeeks

Remove a Character From String in JavaScript

We are given a string and the task is to remove a character from the given string. We have many methods to remove a character from a string that is described below.

Table of Content

  • Method 1: Using JavaScript replace() Method
  • Method 2: Using JavaScript replace() Method with a Regular Expression
  • Method 3: Using JavaScript slice() Method
  • Method 4: Using JavaScript substr() method
  • Method 5: Using JavaScript split() and join() methods
  • Method 6: Using JavaScript splice() Method

Similar Reads

Method 1: Using JavaScript replace() Method

The replace() method replaces the first occurrence of a specified character/string with another character/string....

Method 2: Using JavaScript replace() Method with a Regular Expression

This method is used to remove all occurrences of a specified character or string from the input string, unlike the previous method, which removes only the first occurrence....

Method 3: Using JavaScript slice() Method

The slice() method is used to extract parts of a string between specified indices....

Method 4: Using JavaScript substr() method

The substr() method is used to remove a character from a specific index within the string....

Method 5: Using JavaScript split() and join() methods

The split() method is used to split a string into an array of substrings based on a specified separator. By splitting the string at the character to be removed, and then joining the array elements back into a string, the desired character can be effectively removed....

Method 6: Using JavaScript splice() Method

The splice() method changes the contents of a string by removing or replacing existing elements and/or adding new elements. We can use this method to remove a character at a specified index from the string....