This article will cater to a generic requirement to remove the last comma from a string in javascript using different methods and example illustrations.

Table of Contents:-

Javascript remove last comma (‘,’) from a string using replace() and RegExp

Javascript’s replace() method finds a pattern and replaces some or all of its occurrences with a replacement(character/string). The pattern can be a character or a string, or regExp.

RegExp is the regular expression object. These objects are patterns used to match character combinations in strings.

Example:-

Remove last comma (‘,’) from the string “This is a dummy string, for example purposes,”

Code:-

const dummyString = "This is a dummy string, for example purposes,"
let stringWithoutComma =  dummyString.replace(/,*$/, "")
console.log("Original String: "+ dummyString )
console.log("Final String: "+ stringWithoutComma)

Explanation:-

The above code is using the replace() method with the regular expression as the first argument. This regular expression (/,*$/) searches for a comma at the end of the string and replaces it with the replacement passed as the second argument.

  • / specifies the beginning and end of the regular expression.
  • $ at the end states the end of the string.
  • * signifies 0 or more.

Output:-

Original String: This is a dummy string, for example purposes,
Final String: This is a dummy string, for example purposes

Javascript remove last comma (‘,’) from a string using slice()

The slice(beginIndex, endIndex) method of javascript returns a copy as a new string. The beginIndex and endIndex describe from where the extraction needs to be started and ended. 

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

Example:-

Remove last comma (‘,’) from the string “This is a dummy string, for example purposes,”

Code:-

let dummyString = "This is a dummy string, for example purposes,"
let dummyStringTrimmed = dummyString.trim();
let lastCharValue = dummyStringTrimmed.slice(-1);
 if (lastCharValue == ',' )
    { 
        console.log("Original String: " + dummyString)
        console.log("Final String: " + dummyStringTrimmed.slice(0, -1)) 
    }

Explanation:-

The above code is using the trim() method to remove extra spaces if there are any. Then slice(-1) is used to get the last character of the string. Finally, the code checks if the last character retrieved is a (,). If yes, only then print the original and the new value of string. Else do nothing.

The new value of the string is retrieved by the expression dummyStringTrimmed.slice(0, -1), which will return a new string (original string without the last character).

Output:-

Original String: This is a dummy string, for example purposes,
Final String: This is a dummy string, for example purposes

Javascript remove last comma (‘,’) from a string using substring() and indexOf()

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

The indexOf(searchString, position) is used to get the index of the first occurrence of the specified substring within the string. The first parameter is the substring to search for in the string. The second parameter is the index at which to begin searching the string object.

Example:-

Remove last comma (‘,’) from the string “This is a dummy string, for example purposes,”

Code:-

let dummyString = "This is a dummy string, for example purposes,"
let finalString = ""
if (dummyString.indexOf(',', this.length - ','.length) !== -1) {
    finalString = dummyString.substring(0, dummyString.length - 1)
}
console.log("Original String: " + dummyString)
console.log("Final String: " + finalString)

Explanation:-

The above code is using the indexOf() method to get the index of the comma. If comma (,) is the last character of the string, then feed the variable finalString with a new string where the last comma is removed from the previous string using the substring() method. Else do nothing. In the end, print values of both original and the final string.

Output:-

Original String: This is a dummy string, for example purposes,
Final String: This is a dummy string, for example purposes

Read More:

We hope this article helped you to delete the last comma from the javascript string. Good Luck !!!