Remove trailing comma from comma-separated string

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

    To remove the ", " part which is immediately followed by end of string, you can do:

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

    This handles the empty list (empty string) gracefully, as opposed to lastIndexOf / substring solutions which requires special treatment of such case.

    Example code:

    String str = "kushalhs, mayurvm, narendrabz, ";
    str = str.replaceAll(", $", "");
    System.out.println(str);  // prints "kushalhs, mayurvm, narendrabz"
    

    NOTE: Since there has been some comments and suggested edits about the ", $" part: The expression should match the trailing part that you want to remove.

    • If your input looks like "a,b,c,", use ",$".
    • If your input looks like "a, b, c, ", use ", $".
    • If your input looks like "a , b , c , ", use " , $".

    I think you get the point.

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

    You can do something like this using 'Java 8'

    private static void appendNamesWithComma() {
        List<String> namesList = Arrays.asList("test1", "tester2", "testers3", "t4");
        System.out.println(namesList.stream()
                                    .collect(Collectors.joining(", ")));
    
    }
    
    0 讨论(0)
  • 2020-12-04 21:41

    I'm late on this thread but hope it will help to some one.......

    String abc = "kushalhs , mayurvm , narendrabz ,";
    
    if(abc.indexOf(",") != -1){
        abc = abc.substring(0,abc.length() - 1);
    }
    
    0 讨论(0)
  • 2020-12-04 21:42

    Check if str.charAt(str.length() -1) == ','. Then do str = str.substring(0, str.length()-1)

    0 讨论(0)
  • 2020-12-04 21:45
    (^(\s*?\,+)+\s?)|(^\s+)|(\s+$)|((\s*?\,+)+\s?$)
    

    Regexr

    ex:

    a, b, c
    , ,a, b, c, 
    ,a, b, c   ,
    ,,a, b, c, ,,,
    , a, b, c,    ,
        a, b, c     
         a, b, c ,,
    , a, b, c, 
    , ,a, b, c, ,
     , a, b, c , 
    ,,, a, b, c,,, 
    ,,, ,,,a, b, c,,, ,,,
    ,,, ,,, a, b, c,,, ,,,
     ,,,a, b, c ,,,
     ,,,a, b, c,,, 
       a, b, c   
    

    becomes:

    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    a, b, c
    
    0 讨论(0)
  • 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);
        }
    
    0 讨论(0)
提交回复
热议问题