user1071753 user1071753 - 1 year ago 50
Java Question

how to remove comma if string having comma at end

I want to remove comma at end if string ends with comma

For example

String names = "A,B,C,"

i need to remove last comma if string ends with ","

Answer Source

You could try a regular expression:

names = names.replaceAll(",$", "");

Or a simple substring:

if (names.endsWith(",")) {
  names = names.substring(0, names.length() - 1);