Javascript: Remove First and Last Character from String

This article will remove leading and trailing characters from a javascript string using different methods and example illustrations. While developing in any language, removing the first and last characters from the string is common before processing it, as it might be an address or URL. We will discuss both cases: if the character is known or unknown.

Table of Contents:-

Javascript String Remove First and Last Character using substring()

Javascript’s substring() method will return a part of the string between the first and last indexes passed as arguments or to the end of the string where the index starts from zero.

syntax:

substring(startingIndex)
substring(startingIndex, endingIndex)
  • Here, the character at startingIndex is included in the returned string. 
  • The character at endingIndex is not included in the returned string. 
  • The endingIndex is optional, and if not specified, the substring is returned to the end of the string starting from the startingIndex.

Example:-

Remove the first and last character from the string “/javascript is popular$”

Code:-

var dummyString = "/javascript is popular$";
//startingIndex is 1 and endingIndex is 1 less than the length of the string.
var finalString = dummyString.substring(1, dummyString.length-1);
console.log("Original String: " + dummyString);
console.log("Final String: " + finalString);

Here in the above code, we are using substring() method. The startingIndex is 1 and the endingIndex is 1 less than the length of the original string. Since the index starts from 0, the first character will be removed in the returned string. Also, because the endingIndex is excluded from the returned string, the last character will also be removed.

Output:-

Original String: /javascript is popular$
Final String: javascript is popular

Javascript String Remove First and Last Character from String using slice()

Javascript’s slice() method will extract a part of the string and return it as output. This method will not modify the original string. The index starts from zero.

syntax:

slice(startingIndex)
slice(startingIndex, endingIndex)
  • Here, the character at startingIndex is included in the returned string.
  • The character at endingIndex is excluded in the returned string.
  • If the startingIndex is negative, the extraction starts from originalString.length + startingIndex
  • If the endingIndex is negative, it is considered as originalString.length + endingIndex

Example:-

Remove the first and last character from the string “/javascript is popular$”

Code:-

var dummyString = "/javascript is popular$";
//startingIndex is 1 and endingIndex is -1.
var finalString = dummyString.slice(1,-1);
console.log("Original String: " + dummyString);
console.log("Final String: " + finalString);

Here the above code uses the slice() method to get the extract of the string. The startingIndex is 1 and endingIndex is -1 which will be considered as 21 + (-1) that is 20. Therefore, the extraction is done for slice(1,20) which is: “javascript is popular” . 

Output:-

Original String: /javascript is popular$
Final String: javascript is popular

Javascript String Remove First and Last Character if the Character is known

Javascript’s replace() method finds a pattern and replaces some or all of its occurrences with a replacement(character/string). The pattern can be a character or a string, or regExp.

RegExp is the regular expression object. These objects are patterns used to match character combinations in strings.

Example:-

Remove Backslash (\) and Forward Slash (/) from the string “/javascript is popular/”

Code:-

var dummyString = "/javascript is popular/";
var finalString = dummyString.replace(/^\//g,'').replace(/\/$/g,'');
console.log("Original String: " + dummyString);
console.log("Final String: " + finalString);
  • Here in the above code, we are using replace() method with RegExp. Since we know that backslash(\) and Forward Slash(/) need to be removed, we are replacing them with nothing(”) 
  • The regular expression used is /^\//g and /\/$/g. 
  • Firstly, replace \ or / from the beginning of the string. Then with the next replace() method, remove the \ or / from the end of the string.
  • ^ specifies the beginning of the string
  • / and / marks the beginning and end of the string.
  • \ and / are the forward slash and backslash.
  • $ specifies global search that is to match all occurrences within the string.

Output:-

Original String: /javascript is popular$
Final String: javascript is popular

I hope this article helped you remove the first and last character from a string in javascript. 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