user1071753 user1071753 - 4 months ago 14
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

You could try a regular expression:

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

Or a simple substring:

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