Remove Last Comma or Other Characters from the End of a String

Apex

Removing the last comma is often needed when dynamically building a SOQL Select string. Salesforce quietly released a bunch of new string methods in Winter ’13 that I have started to really appreciate. One of the new methods is removeEnd() and removeEndIngoreCase(). These are great for removing that last comma.

If your data structure is a simple list of strings, you don’t even need the removeEnd(). The String.join() will form the string without an ending comma.

Javascript

Here is how to remove the last comma of a string variable in Javascript.