Javascript: Remove last character of string

While working in javascript, we often need to remove the last character from a string. This article will illustrate how to delete the last character of a javascript string using different methods and examples.

Table of Contents:-

Javascript remove first character from a string using substring()

Javascript’s substring() returns a subset of the string between the start and end indexes or to the end of the string. 

Code:-

let dummyString = "Javascript*";
myStringFinal = dummyString.substring(0, dummyString.length - 1)
console.log("Original String: "+ dummyString )
console.log("New String: "+ myStringFinal)

Here the first index is 0, and the last index is the original string -1. Therefore, as a result, we get the last character chopped off from the original string.

Output:-

Original String: Javascript*
Final String: Javascript

Javascript remove last character from a string using slice()

The slice(beginIndex, endIndex) method of javascript returns a copy of a string as a new string. The slice() function does not modify the original string. The begin and end index specify from where the extraction needs to be started and ended, respectively. 

If any of the indexes are negative, it is considered -> string. length – index.

The slice() and the substring() methods of javascript are all almost the same with the exception that slice() can accept a negative index relative to the end of the string while substring() cannot and, if done, will throw an out of bound error.

Code:-

let dummyString = "Javascript*";
myStringFinal = dummyString.slice(0, -1); 
console.log("Original String: "+ dummyString )
console.log("Final String: "+ myStringFinal)

Here, myStringFinal will hold the value -> last character chopped off from the original string.

Output:-

Original String: Javascript*
Final String: Javascript

Javascript remove last character of string if comma

The below function will remove the last character of the string only if it is a comma(‘,’)

Function Code:-

function deleteLastComma(_string){        
    let lastPosComma =_string.lastIndexOf(",")
    let finalString =_string.substring(0,lastPosComma) 
    return finalString
}

Usage:-

let dummyString = "Javascript,"
console.log("Original String: "+ dummyString )
console.log("String Without Last Comma: "+ deleteLastComma(dummyString))

Output:-

Original String: Javascript,
String Without Last Comma: Javascript

OR

Code:-

let dummyString = "Javascript,"
stringWithoutComma = dummyString.replace(/,\s*$/, "")
console.log("Original String: "+ dummyString )
console.log("String Without Last Comma: "+ stringWithoutComma)

Output:-

Original String: Javascript,
String Without Last Comma: Javascript

Javascript remove last character from string if exists / if slash

The below section will delete the last character only if a specific character exists in the string at the end. In the example below, we check for slash (‘/’) and deleting it off if found in the end.

Code using substring():-

let  dummyString = "Javascript/"
let  stringFinal = ""
if(dummyString[dummyString.length-1] === "/") {
    stringFinal = dummyString.substring(0, dummyString.length-1);
}
console.log("Original String: "+ dummyString )
console.log("Final String: "+ stringFinal)

OR

Code using slice():-

let  dummyString = "Javascript/"
let  stringFinal = ""
if(dummyString.slice(-1) == "/"){
stringFinal = dummyString.slice(0,-1)+ ""
console.log("Original String: "+ dummyString )
console.log("Final String: "+ stringFinal)
}

OR

Code using regex:-

let  dummyString = "Javascript/"
let  stringFinal = ""
stringFinal = dummyString.replace(/\/$/, '')
console.log("Original String: "+ dummyString )
console.log("Final String: "+ stringFinal)

Output:-

Original String: Javascript/
Final String: Javascript

Javascript remove last character from string if space

Remove all spaces from end:-

let  dummyString = "Javascript   "
let  stringFinal1 = ""
stringFinal1 = dummyString.replace(/\s*$/,'')
console.log(stringFinal1 + "*Just For Testing")

Output:-

Javascript*Just For Testing

All spaces from the end of the “Javascript” string are removed.

Remove just one space from end:-

let  dummyString = "Javascript   "
let  stringFinal = ""
stringFinal = dummyString.replace(/\s?$/,'')
console.log(stringFinal + "*Just For Testing")

Output:-

Javascript  *Just For Testing

Only one space from the end of the “Javascript” string is removed, and two spaces are still left, as shown in the output.

Remove spaces from end using trimEnd():

The trimEnd() method of javascript will remove all the whitespaces from the end of the string.

let  dummyString = "Javascript   "
let  stringFinal = ""
stringFinal = dummyString.trimEnd()
console.log(stringFinal + "*Just For Testing")

Output:-

Javascript*Just For Testing

Read More:

We hope this article helped you to delete the last character from the javascript string. 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