Javascript: Remove substring from end of a string

We often come across a requirement to remove a substring from the end of a string to process it. This article will illustrate simple ways to remove a substring from the end of a string using different methods and examples.

Table of Contents:-

Remove substring from end of a string in javascript using replace()

Example:-

Remove ‘.00’ from the end of the string “890909.00”

Code:-

let dummyString="890909.00"
dummyString = dummyString.replace(/(.00$)/, '')
console.log(dummyString)

Explanation:-

Here, we are using the replace() javascript method that looks for ‘.00’ at the end of the string and replaces it with (”), which means nothing. Regular Expression /(.00$)/ is passed as the first argument. Where,

  • / and / mark the beginning and end of the pattern.
  • .00$  specifies to match ‘.00’ at the end of the string

Output:-

890909

Remove substring from end of a string in javascript using substring()

Example:-

Remove ‘.00’ from the end of the string “890909.00”

Code:-

let dummyString ="890909.00"
let substringToRemove = ".00"
dummyString = dummyString.substring(0, dummyString.length - substringToRemove.length )
console.log(dummyString)

Explanation:-

Here, we are using javascript’s substring(startIndex, endIndex) method to extract a substring from the original string based on the start and end indexes passed as parameters. The index is zero-based.

  • The startIndex specifies from where the extracted substring should begin. In our case, the value of startIndex is zero. That is, it should start from the beginning.
  • The endIndex specifies till which character the extracted substring will be formed, excluding the character at the endIndex.The endIndex value is optional. In our case the value of endIndex is -> length of the original string subtract length of the substring.

Output:-

890909

Remove substring from end of a string in javascript using slice()

Example:-

Remove ‘.00’ from the end of the string “890909.00”

Code:-

let dummyString ="890909.00"
let substringToRemove = ".00"
dummyString = dummyString.slice(0,-(substringToRemove.length))
console.log(dummyString)

Explanation:-

Here, we are using the slice(startIndex, endIndex) method of javascript to return a portion of the original string. The index is zero-based.

  • The startIndex specifies from where the extraction should begin. In our case, the value of startIndex is zero.
  • The endIndex specifies that the extraction will end just before the endIndex and is an optional value. In our case, the endIndex value is negative , hence the endIndex value will be string.length + endIndex that is 8+(-2) -> 6 as per the functioning of slice() method.

Output:-

890909

Remove substring after a particular character from end of a string in javascript

Example:-

Remove everything after dot (.) from the string “890909.00”

Code:-

let dummyString = "890909.00";
dummyString= dummyString.slice(0, dummyString.lastIndexOf("."))
console.log(dummyString)

Explanation:-

Again we are using the slice(startIndex, endIndex) method.

  • In our case, the value of startIndex is zero.
  • In our case, the endIndex value is the value of the index of the last dot (.)

Output:-

890909

Read More:

We hope this article helped you to delete a substring from the end of 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