user1071753 user1071753 - 2 years ago 139
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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download