Remove last part of string JavaScript

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’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:-

Advertisements

Remove the last 8 characters from the string “Javascript is popular language”

Code:-

Read More:

  • Remove first and last double quotes from a string in…
  • Javascript: Remove all but numbers from string
  • Javascript: Remove substring from start of a string
  • Javascript: Remove substring from end of a string

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 !!!

How to remove last substring from string in JavaScript?

Remove the Last Character from a String in JavaScript.
Using substring() method. substring() method in javascript is used to extract and return a desired part of string and by using this we can remove last character from string javascript. ... .
Using slice() method. ... .
Using substr() method. ... .
Use the replace() Method..

How do you remove the last part of a string?

We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove.

How do I remove a specific part of a string in JavaScript?

JavaScript String replace() The replace() method takes two parameters, the first of which is the character to be replaced and the second of which is the character to replace it with. This method replaces the first occurrence of the character. To remove the character, we could give the second parameter as empty.

How to remove last 3 characters from string in JavaScript?

slice(0, -n) . Where n is the number of characters you want to truncate. Save this answer.