Remove trailing comma from comma-separated string

前端 未结 16 882
生来不讨喜
生来不讨喜 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:04

    Or something like this:

    private static String myRemComa(String input) { 
            String[] exploded = input.split(",");
            input="";
            boolean start = true;
            for(String str : exploded) {
    
             str=str.trim();
             if (str.length()>0) {
                 if (start) {
                     input = str;
                        start = false;
                    } else {
                        input = input + "," + str;
                    }
             }
            }
    
            return input;
        }
    

提交回复
热议问题