Java List to String convertListToString(List list, String delimiter, char encloser)

Here you can find the source of convertListToString(List list, String delimiter, char encloser)

Description

Converts list to string concatenating list elements with the delimiter.

License

Apache License

Parameter

Parameter Description
list List to convert to string
delimiter Delimiter to put between elements
encloser Character to enclose each element

Return

List members, delimited by specifed delimiter. Each element enclosed with specified character

Declaration

public static String convertListToString(List list, String delimiter, char encloser) 

Method Source Code

//package com.java2s;
/*//from w w  w. j  av a  2  s. c  o  m
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

import java.util.*;

public class Main {
    /**
     * Converts list to string concatenating list elements with the delimiter
     *
     * @param list      List to convert to string
     * @param delimiter Delimiter to put between elements
     * @return List members, delimited by specifed delimiter.
     */
    public static String convertListToString(List list, String delimiter) {
        return convertListToString(list, delimiter, null);
    }

    /**
     * Converts list to string concatenating list elements with the delimiter.
      * Each element is additionally enclosed in <code>encloser</code> chars.
     *
     * @param list      List to convert to string
     * @param delimiter Delimiter to put between elements
     * @param encloser  Character to enclose each element
     * @return List members, delimited by specifed delimiter. Each element enclosed with specified character
     */
    public static String convertListToString(List list, String delimiter, char encloser) {
        return convertListToString(list, delimiter, new Character(encloser));
    }

    /**
     * Converts list to string concatenating list elements with the delimiter.
      * Each element is additionally enclosed in <code>encloser</code> chars.
     *
     * @param list      List to convert to string
     * @param delimiter Delimiter to put between elements
     * @param encloser  Character to enclose each element
     * @return List members, delimited by specifed delimiter. Each element enclosed with specified character
     */
    private static String convertListToString(List list, String delimiter, Character encloser) {

        if (list == null || list.size() == 0) {
            return new String();
        }

        StringBuffer sb = new StringBuffer();
        for (Iterator i = list.iterator(); i.hasNext();) {
            String next = String.valueOf(i.next());
            if (encloser != null) {
                next = encloser + next + encloser;
            }
            sb.append(next);
            if (i.hasNext()) {
                sb.append(delimiter);
            }
        }

        return sb.toString();
    }
}

Related

  1. asStringList(List coll)
  2. asStringList(Set attributes)
  3. convertListToStrArray(List list)
  4. convertListToString(Iterable l)
  5. convertListToString(List list)
  6. convertListToString(List list)
  7. convertListToString(List data)
  8. convertListToString(List list, char delimiter, int estimatedLength)
  9. convertListToString(List dests)

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