Java Collection to String toString(Collection c, String start, String separator, String end)

Here you can find the source of toString(Collection c, String start, String separator, String end)

Description

An flexible alternative for converting a Collection to a String.

License

Open Source License

Parameter

Parameter Description
c The Collection to convert to a String
start The String to place at the beginning of the returned String
separator The String to place in between elements of the Collection c.
end The String to place at the end of the returned String

Return

A String which starts with 'start', followed by the elements in the Collection c separated by 'separator', ending with 'end'.

Declaration

@SuppressWarnings("rawtypes")
public static String toString(Collection c, String start, String separator, String end) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing./*w ww  . j av a 2s .c  o  m*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;

public class Main {
    /**
     * An flexible alternative for converting a Collection to a String.
     *
     * @param c The Collection to convert to a String
     * @param start The String to place at the beginning of the returned String
     * @param separator The String to place in between elements of the Collection c.
     * @param end The String to place at the end of the returned String
     * @return A String which starts with 'start', followed by the elements in the Collection c separated by 'separator',
     * ending with 'end'.
     */
    @SuppressWarnings("rawtypes")
    public static String toString(Collection c, String start, String separator, String end) {
        Iterator i = c.iterator();
        StringBuilder myString = new StringBuilder();

        if (start != null) {
            myString.append(start);
        }

        boolean first = true;
        while (i.hasNext()) {
            if (!first) {
                myString.append(separator);
            }
            myString.append(i.next().toString());
            first = false;
        }

        if (end != null) {
            myString.append(end);
        }

        return myString.toString();
    }

    public static String toString(String separator, Object... objects) {
        Collection<Object> objectsCol = new ArrayList<Object>(objects.length);
        for (Object obj : objects) {
            objectsCol.add(obj);
        }
        return toString(objectsCol, null, separator, null);
    }

    @SuppressWarnings("rawtypes")
    public static String toString(String separator, Collection c) {
        return toString(c, null, separator, null);
    }
}

Related

  1. stringify(Collection collection)
  2. toStr(Collection collection)
  3. toStr(Collection elements)
  4. toString(Collection c)
  5. toString(Collection c)
  6. toString(Collection collection, String separator, int fixedLength)
  7. toString(Collection objects)
  8. toString(Collection setOfTests)
  9. toString(Collection softwareTags)