Java Collection Concatenate concat(Collection pieces, String delim)

Here you can find the source of concat(Collection pieces, String delim)

Description

Build a string concatenating the pieces, adding the delim between each.

License

Open Source License

Declaration

public static final String concat(Collection<String> pieces, String delim) 

Method Source Code

//package com.java2s;
/*//from www  . ja va  2  s  .  c  o m
  Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
  This file is part of the Semantic Discovery Toolkit.
    
  The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
    
  The Semantic Discovery Toolkit is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.
    
  You should have received a copy of the GNU Lesser General Public License
  along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
  */

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
       * Build a string concatenating the pieces from startIndex (inclusive) to endIndex
       * (exclusive), adding the delim between each.
       */
    public static final String concat(String[] pieces, String delim, int startIndex, int endIndex) {
        final StringBuilder result = new StringBuilder();

        for (int i = startIndex; i < endIndex; ++i) {
            if (result.length() > 0)
                result.append(delim);
            result.append(pieces[i]);
        }

        return result.toString();
    }

    /**
       * Build a string concatenating the pieces, adding the delim between each.
       */
    public static final String concat(Collection<String> pieces, String delim) {
        return concat(pieces, delim, 0, pieces.size());
    }

    /**
       * Build a string concatenating the pieces from startIndex (inclusive) to endIndex
       * (exclusive), adding the delim between each.
       */
    public static final String concat(Collection<String> pieces, String delim, int startIndex, int endIndex) {
        final StringBuilder result = new StringBuilder();

        int index = 0;
        for (Iterator<String> iter = pieces.iterator(); iter.hasNext();) {
            final String piece = iter.next();
            if (index >= endIndex)
                break;
            if (index < startIndex)
                continue;

            if (result.length() > 0)
                result.append(delim);
            result.append(piece);
        }

        return result.toString();
    }
}

Related

  1. concat(Collection> classes, String separator)
  2. concat(Collection collection, String delimiter)
  3. concat(Collection c)
  4. concat(Collection items)
  5. concat(Collection parts, int start, String defaultText)
  6. concat(Collection a, Collection b)
  7. concat(Collection a, int offset, String sep, String start, String end)
  8. concat(Collection first, Collection second)
  9. concat(Collection... collections)