- How to Remove the Last Character from a String in JavaScript
- Alternative Methods
- Advanced Features
- More Fundamentals Tutorials
- Javascript: Remove substring from end of a string
- Remove substring from end of a string in javascript using replace()
- Frequently Asked:
- Remove substring from end of a string in javascript using substring()
- Remove substring from end of a string in javascript using slice()
- Remove substring after a particular character from end of a string in javascript
- Related posts:
- Share your love
- Leave a Comment Cancel Reply
- Terms of Use
- Disclaimer
- Javascript string remove last string
- # Remove First and Last Characters from a String in JavaScript
- # Remove First and Last Characters from a String using String.substring()
- # Additional Resources
How to Remove the Last Character from a String in JavaScript
To remove the last character from a string in JavaScript, you should use the slice() method. It takes two arguments: the start index and the end index. slice() supports negative indexing, which means that slice(0, -1) is equivalent to slice(0, str.length — 1) .
let str = 'Masteringjs.ioF'; str.slice(0, -1); // Masteringjs.io
Alternative Methods
slice() is generally easier, however other methods available are substring() and replace() . substring() does not have negative indexing, so be sure to use str.length — 1 when removing the last character from the string. replace() takes either a string or a regular expression as its pattern argument. Using /.$/ as the regular expression argument matches the last character of the string, so .replace(/.$/, ») replaces the last character of the string with an empty string.
let str = 'Masteringjs.ioF'; str.substring(0, str.length - 1); // Masteringjs.io str.substr(0, str.length - 1); // Masteringjs.io str.replace(/.$/, ''); // Masteringjs.io
Advanced Features
With replace() , you can specify if the last character should be removed depending on what it is with a regular expression. For example, suppose you want to remove the last character only if the last character is a number. You can use .replace(/\d$/, ») as shown below.
// For a number, use \d$. let str = 'Masteringjs.io0'; str.replace(/\d$/, ''); // Masteringjs.io let str2 = 'Masteringjs.io0F'; // If the last character is not a number, it will not replace. str.replace(/\d$/, ''); // Masteringjs.io0F;
More Fundamentals Tutorials
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()
Remove ‘.00’ from the end of the string “890909.00”
Frequently Asked:
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
Remove substring from end of a string in javascript using substring()
Remove ‘.00’ from the end of the string “890909.00”
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.
Remove substring from end of a string in javascript using slice()
Remove ‘.00’ from the end of the string “890909.00”
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 + endIndexthat is 8+(-2) -> 6 as per the functioning of slice() method.
Remove substring after a particular character from end of a string in javascript
Remove everything after dot (.) from the string “890909.00”
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 (.)
We hope this article helped you to delete a substring from the end of a string in javascript. Good Luck .
Related posts:
Share your love
Leave a Comment Cancel Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Terms of Use
Disclaimer
Copyright © 2023 thisPointer
To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Javascript string remove last string
Last updated: Dec 21, 2022
Reading time · 2 min
# Remove First and Last Characters from a String in JavaScript
To remove the first and last characters from a string, call the slice() method, passing it 1 and -1 as parameters.
The String.slice() method will return a copy of the original string with the first and last characters removed.
Copied!const str = 'abcd'; const withoutFirstAndLast = str.slice(1, -1); console.log(withoutFirstAndLast); // 👉️ bc
We passed the following arguments to the String.slice method:
- start index — the index (zero-based) at which we start extraction.
- end index — extract characters up to, but not including this index. A negative index of -1 means «go up to, but not including the last character in the string».
Passing an end index parameter of -1 and str.length — 1 is the same. We instruct the slice method to go up to, but not including the last character in the string.
The String.slice() method doesn’t mutate the original string, it returns a new string. Strings are immutable in JavaScript.
Calling the slice() method on an empty string doesn’t throw an error, it returns an empty string.
Copied!const str = ''; const withoutFirstAndLast = str.slice(1, -1); console.log(withoutFirstAndLast); // 👉️ ""
# Remove First and Last Characters from a String using String.substring()
Alternatively, you can use the String.substring() method.
Copied!const str = 'abcd'; const withoutFirstAndLast = str.substring(1, str.length - 1); console.log(withoutFirstAndLast); // 👉️ bc
We used the String.substring() method to achieve the same result.
The String.substring() method returns a slice of the string from the start index to the excluding end index.
The method takes the following parameters:
Name | Description |
---|---|
start index | The index of the first character to include in the returned substring |
end index | The index of the first character to exclude from the returned substring |
If no end index is specified the slice goes to the end of the string.
There are a couple of differences between the String.substring() and the String.slice() methods:
- The substring() method swaps its start and end index if the start index is greater than the end index. The slice() method returns an empty string in this case.
Copied!const str = 'bobby'; console.log(str.substring(3, 0)); // 👉️ bob console.log(str.slice(3, 0)); // 👉️ ''
- If either of both arguments passed to substring() are negative, they are treated as if they were 0 .
Copied!const str = 'bobby'; console.log(str.substring(-3)); // 👉️ bobby console.log(str.slice(-3)); // 👉️ bby
When given a negative index, the slice() method counts backward from the end of the string to find the indexes.
My personal preference is to use the String.slice() method because it’s more intuitive than String.substring() in many different scenarios.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.