user1071753 user1071753 - 9 months ago 28
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 ","


You could try a regular expression:

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

Or a simple substring:

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