Java List to String listToString(List l, String sep)

Here you can find the source of listToString(List l, String sep)

Description

Alternate to List.toString() that formats the list as a simple string using a specified string as the separator between elements.

License

Open Source License

Parameter

Parameter Description
l the list of strings to concatenated into a single string.
sep the seperator to inserted between each list element.

Declaration

public static String listToString(List<String> l, String sep) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005-2012 Synopsys, Incorporated
 * 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:// w w  w.j  av a  2s. co  m
 * Synopsys, Inc - Initial implementation 
 *******************************************************************************/

import java.util.List;

public class Main {
    /**
    * Alternate to List.toString() that formats the list
    * as a simple string using a specified string as the
    * separator between elements.
    * @param l the list of strings to  concatenated into a single string.
    * @param sep the seperator to inserted between each list element.
    */
    public static String listToString(List<String> l, String sep) {
        if (l == null)
            return "";
        StringBuffer b = new StringBuffer();
        appendListToString(b, l, sep);
        return b.toString();
    }

    /**
    * Append a list to a string buffer placing a specified
    * string between each element.
    * @param b the string buffer to be filled in.
    * @param l the list of strings to be appended.
    * @param sep the seperator to inserted between each list element.
    */
    public static void appendListToString(StringBuffer b, List<String> l, String sep) {
        if (l != null) {
            for (String element : l) {
                b.append(sep);
                b.append(element);
            }
        }
    }
}

Related

  1. listToString(List list)
  2. listToString(List list)
  3. listToString(List list, String operator)
  4. listToString(List value)
  5. listToString(List args)
  6. listToString(List list)
  7. listToString(List list)
  8. ListToString(List list)
  9. listToString(List list)

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