Java List to String listToString(List list, String separator, String prefix, String suffix)

Here you can find the source of listToString(List list, String separator, String prefix, String suffix)

Description

Creates a string consisting of the string representation of the elements in the list separated by separator.

License

Open Source License

Parameter

Parameter Description
list the list to print
separator to use in separating elements
prefix prepended to each individual element in the list before adding to the returned string.
suffix appended to each individual element in the list before adding to the returned string.

Return

String representing the list

Declaration

public static String listToString(List<?> list, String separator, String prefix, String suffix) 

Method Source Code

//package com.java2s;
/*/*  ww  w  .  j  av  a 2 s  .c o  m*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2011-2015 ForgeRock AS
 */

import java.util.List;

public class Main {
    /**
     * Creates a string consisting of the string representation of the elements in
     * the <code>list</code> separated by <code>separator</code>.
     *
     * @param list
     *          the list to print
     * @param separator
     *          to use in separating elements
     * @param prefix
     *          prepended to each individual element in the list before adding to
     *          the returned string.
     * @param suffix
     *          appended to each individual element in the list before adding to
     *          the returned string.
     * @return String representing the list
     */
    public static String listToString(List<?> list, String separator, String prefix, String suffix) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            if (prefix != null) {
                sb.append(prefix);
            }
            sb.append(list.get(i));
            if (suffix != null) {
                sb.append(suffix);
            }
            if (i < list.size() - 1) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }
}

Related

  1. listToString(List items)
  2. listToString(List l, String delim)
  3. listToString(List l, String sep)
  4. listToString(List list)
  5. listToString(List list)
  6. listToString(List list, String seperator)
  7. listToString(List list)
  8. listToString(List list)
  9. listToString(List list, String operator)

    HOME | Copyright © www.java2s.com 2016