Java Collection Join joinStrings(String delim, Collection strs)

Here you can find the source of joinStrings(String delim, Collection strs)

Description

Joins a collection of strings together sepeated by a delim in the middle

License

Open Source License

Declaration

static public String joinStrings(String delim, Collection<String> strs) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Collection;

public class Main {
    /**/* w  ww  .  jav  a  2  s.  c  o  m*/
     * Joins a list of strings together sepeated by a delim in the middle
     */
    static public String joinStrings(String delim, String... strs) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strs.length - 1; i++) {
            sb.append(strs[i]);
            sb.append(delim);
        }
        sb.append(strs[strs.length - 1]);
        return sb.toString();
    }

    /**
     * Joins a collection of strings together sepeated by a delim in the middle
     */
    static public String joinStrings(String delim, Collection<String> strs) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (String str : strs) {
            if (first) {
                first = false;
            } else {
                sb.append(delim);
            }
            sb.append(str);
        }
        return sb.toString();
    }
}

Related

  1. joinString(Collection values, String delimiter)
  2. joinStringCollection(Collection collection, String separator)
  3. joinStrings(Collection strings)
  4. joinStrings(Collection names)
  5. joinStrings(Collection strings)
  6. joinStringValues(Collection values)
  7. joinTo(Collection data, String sep, StringBuilder sb)
  8. joinTogether(Collection items, String delim)
  9. joinToStringBuilder(Collection collection, String separator)

  10. HOME | Copyright © www.java2s.com 2016