Remove trailing comma from comma-separated string

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

    You can do something like using join function of String class.

    import java.util.Arrays;
    import java.util.List;
    
    public class Demo {
    
        public static void main(String[] args) {
            List items = Arrays.asList("Java", "Ruby", "Python", "C++");
            String output = String.join(",", items);
            System.out.println(output);
        }
    
    }
    

提交回复
热议问题