Many times we come across a requirement to remove the last N characters from a string in javascript. This article will discuss removing a few characters from the string based on the value we pass for N using different examples illustrations.
Table of Contents:-
- Javascript remove last N characters from a string using substring()
- Javascript remove last N characters from a string using slice()
Javascript remove last N characters from a string using substring()
Javascript’s substring(startIndex, endIndex) method returns a substring from the original string based on the start and end indexes passed as parameters.
- The startIndex is the first argument that specifies from where the substring should begin. The character at the startIndex is included in the substring.
- The endIndex is the second argument to the method and is optional. The endIndex specifies till which character the substring will be formed, excluding the character at the endIndex.
Syntax:-
<original_string>.substring(0, <original_string>.length - N)
Example1:-
Frequently Asked:
Remove the last 8 characters from the string “Javascript is popular language”
Code:-
let dummyString = "Javascript is popular language" let finalString = dummyString.substring(0, dummyString.length - 8) console.log("original string: " + dummyString) console.log("final string: " + finalString)
Explanation:-
The above code uses the substring() method to retrieve a portion of the string from the original, starting from the index position 0 till -> length of the string-8. The value of N, in this case, is 8.
Output:-
original string: Javascript is popular language final string: Javascript is popular
Example2:-
Remove the last 3 characters from the string “Javascript123”
Code:-
let dummyString = "Javascript123" let finalString = dummyString.substring(0, dummyString.length - 3) console.log("original string: " + dummyString) console.log("final string: " + finalString)
The value of N, in this case, is 3.
Output:-
original string: Javascript123 final string: Javascript
Javascript remove last N characters from a string using slice()
Javascript’s slice(startIndex, endIndex) method returns a new String object part of the original string. The slice method will not modify the original string.Â
- The startIndex is the first argument that specifies from where the extraction should begin. The index is zero-based, and if the value is negative, it means -> length of the original string + startIndex
- The endIndex is the second argument to the method and is optional. The extraction will end before the endIndex.
Syntax:-
<original_string>.slice(0, -N)
Example1:-
Remove the last 8 characters from the string “Javascript is popular language”
Code:-
let dummyString = "Javascript is popular language" let finalString = dummyString.slice(0,-8) console.log("original string: " + dummyString) console.log("final string: " + finalString)
Explanation:-
The above code is using the slice() method to retrieve a portion from the original starting, which starts from the character at 0th index till the index -> (dummyString + (-8)).
Output:-
original string: Javascript is popular language final string: Javascript is popular
Example2:-
Remove the last 3 characters from the string “Javascript123”
Code:-
let dummyString = "Javascript123" let finalString = dummyString.slice(0, -3) console.log("original string: " + dummyString) console.log("final string: " + finalString)
The value of N, in this case, is 3.
Output:-
original string: Javascript123 final string: Javascript
Read More:
- Javascript remove line breaks from string (4 ways)
- Remove special characters from a string in python
- Javascript: How to remove text from string
- Javascript: Remove last character of string
- Javascript: Remove first character from a string
We hope this article helped you to delete the last N characters from a string in javascript. Good Luck !!!