Remove trailing comma from comma-separated string

前端 未结 16 886
生来不讨喜
生来不讨喜 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 21:47

    i am sharing code form my project using regular expression you can do this...

    String ChildBelowList = "";
    
        if (!Childbelow.isEmpty()) {
            for (int iCB = 0; iCB < Childbelow.size(); iCB++) {
    
                ChildBelowList = ChildBelowList += Childbelow.get(iCB) + ",";
    
    
    
            }
            ChildBelowList = ChildBelowList.replaceAll("(^(\\s*?\\,+)+\\s?)|(^\\s+)|(\\s+$)|((\\s*?\\,+)+\\s?$)", "");
    
            tv_childbelow.setText(ChildBelowList);
    
        } else {
            ll_childbelow.setVisibility(View.GONE);
        }
    

提交回复
热议问题