Remove trailing comma from comma-separated string

前端 未结 16 898
生来不讨喜
生来不讨喜 2020-12-04 21:37

I got String from the database which have multiple commas (,) . I want to remove the last comma but I can\'t really find a simple way of doing it.

16条回答
  •  爱一瞬间的悲伤
    2020-12-04 22:05

    You can try with this, it worked for me:

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

    Or you can try with this too:

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

提交回复
热议问题