Remove trailing comma from comma-separated string

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

    For more than one commas

                String names = "Hello,World,,,";
        System.out.println(names.replaceAll("(,)*$", ""));
    

    Output: Hello,World

    0 讨论(0)
  • 2020-12-04 21:50

    And one more ... this also cleans internal commas mixed with whitespace:

    From , , , ,one,,, , ,two three, , , ,,four, , , , , to one,two three, four

    text.replaceAll("^(,|\\s)*|(,|\\s)*$", "").replaceAll("(\\,\\s*)+", ",");
    
    0 讨论(0)
  • 2020-12-04 21:52
    public static String removeExtraCommas(String entry) {
        if(entry==null)
            return null;
    
        String ret="";
        entry=entry.replaceAll("\\s","");
        String arr[]=entry.split(",");
        boolean start=true;
        for(String str:arr) {
            if(!"".equalsIgnoreCase(str)) {
                if(start) {
                    ret=str;
                    start=false;
                }
                else {
                ret=ret+","+str;
                }
            }
        }               
        return ret;
    
    }
    
    0 讨论(0)
  • 2020-12-04 21:54
        String str = "kushalhs , mayurvm , narendrabz ,";
        System.out.println(str.replaceAll(",([^,]*)$", "$1"));
    
    0 讨论(0)
  • 2020-12-04 21:55

    Use Guava to normalize all your commas. Split the string up around the commas, throw out the empties, and connect it all back together. Two calls. No loops. Works the first time:

    import com.google.common.base.Joiner;
    import com.google.common.base.Splitter;
    
    public class TestClass {
    
        Splitter splitter = Splitter.on(',').omitEmptyStrings().trimResults();
        Joiner joiner = Joiner.on(',').skipNulls();
    
        public String cleanUpCommas(String string) {
            return joiner.join(splitter.split(string));
        }
    
    }
    
    
    
    public class TestMain {
    
        public static void main(String[] args) {
            TestClass testClass = new TestClass();
    
            System.out.println(testClass.cleanUpCommas("a,b,c,d,e"));
            System.out.println(testClass.cleanUpCommas("a,b,c,d,e,,,,,"));
            System.out.println(testClass.cleanUpCommas("a,b,,, ,c,d,  ,,e,,,,,"));
            System.out.println(testClass.cleanUpCommas("a,b,c,d,  e,,,,,"));
            System.out.println(testClass.cleanUpCommas(",,, ,,,,a,b,c,d,  e,,,,,"));
        }
    
    }
    

    Output:

    a,b,c,d,e
    a,b,c,d,e
    a,b,c,d,e
    a,b,c,d,e
    a,b,c,d,e

    Personally, I hate futzing around with counting limits of substrings and all that nonsense.

    0 讨论(0)
  • 2020-12-04 21:59

    You can use this:

    String abc = "kushalhs , mayurvm , narendrabz ,";
    String a = abc.substring(0, abc.lastIndexOf(","));
    
    0 讨论(0)
提交回复
热议问题