What's the best way to build a string of delimited items in Java?

前端 未结 30 2247
耶瑟儿~
耶瑟儿~ 2020-11-22 05:36

While working in a Java app, I recently needed to assemble a comma-delimited list of values to pass to another web service without knowing how many elements there would be i

30条回答
  •  臣服心动
    2020-11-22 06:26

    So basically something like this:

    public static String appendWithDelimiter(String original, String addition, String delimiter) {
    
    if (original.equals("")) {
        return addition;
    } else {
        StringBuilder sb = new StringBuilder(original.length() + addition.length() + delimiter.length());
            sb.append(original);
            sb.append(delimiter);
            sb.append(addition);
            return sb.toString();
        }
    }
    

提交回复
热议问题