How to use String substring() Method In Javascript

The string.substring() method returns the part of a given string from the start index to the end index. Indexing always start from zero (0).

Syntax:

string.substring(Startindex, Endindex)

Example: In this example, we will remove the first and the last character of the string using the substring() method in JavaScript.

Javascript
// JavaScript Program to remoove first 
// and last character of a string
function removeFirstLast(str) {
    return str.substring(1, str.length - 1);
}

// Driver code
const str = 'w3wiki';

console.log(removeFirstLast(str));

Output
eeksforGeek

JavaScript Program to Remove First and Last Characters from a String

This article will show you how to remove the first and last characters from a given string in JavaScript. There are two methods to remove the first and last characters in a string.

Table of Content

  • 1. Using String slice() Method
  • 2. Using String substring() Method
  • 3. Using Array Methods
  • 4. Using String substr() Method:
  • 5. Using Regular Expression and String Replace Method:


Similar Reads

1. Using String slice() Method

The string.slice() method returns a part or slice of the given input string....

2. Using String substring() Method

The string.substring() method returns the part of a given string from the start index to the end index. Indexing always start from zero (0)....

3. Using Array Methods

To remove the first and last characters from a string using array methods, you can convert the string to an array, remove the first and last elements with shift() and pop(), then join the array back into a string....

4. Using String substr() Method:

The substr() method returns the characters in a string beginning at the specified location through the specified number of characters....

5. Using Regular Expression and String Replace Method:

In this method, we can utilize a regular expression along with the string replace() method to remove the first and last characters from a string....

Contact Us