how to remove comma if string having comma at end
if (names.endsWith(",")) {
names = names.substring(0, names.length()-1);
}
int last = names.length() - 1;
if (last > 0 && names.charAt(last) == ',') {
names = names.substring(0, last);
}
You could try a regular expression:
names = names.replaceAll(",$", "");
Or a simple substring:
if (names.endsWith(",")) {
names = names.substring(0, names.length() - 1);
}