Java Array Implode implode(String[] data, String glue)

Here you can find the source of implode(String[] data, String glue)

Description

Converts a list of strings into a long string separated by glue.

License

LGPL

Parameter

Parameter Description
data string array
glue a parameter

Return

String

Declaration

public static String implode(String[] data, String glue) 

Method Source Code

//package com.java2s;
/*//  w  ww. java 2s.  co m
 *   This software is distributed under the terms of the FSF 
 *   Gnu Lesser General Public License (see lgpl.txt). 
 *
 *   This program is distributed WITHOUT ANY WARRANTY. See the
 *   GNU General Public License for more details.
 */

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Main {
    /**
     * Converts a list of strings into a long string separated by glue.
     * 
     * @param data string array
     * @param glue
     * @return String
     */
    public static String implode(String[] data, String glue) {
        if (data == null || data.length == 0)
            return "";

        StringBuilder sb = new StringBuilder();
        int total = data.length;
        for (int i = 0; i < total; i++) {
            String item = data[i];
            sb.append(item).append(glue);
        }

        String result = sb.toString();
        if (result.endsWith(glue))
            result = result.substring(0, result.lastIndexOf(glue));
        return result;
    }

    /**
     * Converts a list of strings into a long string separated by glue.
     * 
     * @param data a list of strings
     * @param glue
     * @return String
     */
    public static String implode(List<String> data, String glue) {
        if (data == null || data.size() == 0)
            return "";

        StringBuilder sb = new StringBuilder();
        Iterator<String> it = data.iterator();
        while (it.hasNext()) {
            String item = it.next();
            sb.append(item).append(glue);
        }

        String result = sb.toString();
        if (result.endsWith(glue))
            result = result.substring(0, result.lastIndexOf(glue));
        return result;
    }

    /**
     * Converts map into a long string separated by glue.
     * 
     * @param data a map
     * @param glue
     * @return String
     */
    public static String implode(Map<String, ?> data, String glue) {
        if (data == null || data.size() == 0)
            return "";

        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, ?> entry : data.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue())
                    .append(glue);
        }

        String result = sb.toString();
        if (result.endsWith(glue))
            result = result.substring(0, result.lastIndexOf(glue));
        return result;
    }
}

Related

  1. implode(Object[] array, String separator)
  2. implode(Object[] ary, String delim)
  3. implode(String delim, Object[] objects)
  4. implode(String delim, String[] args)
  5. implode(String[] array, String separator)
  6. implode(String[] inputArray, String glueString)
  7. implode(String[] values, String separator, String beforeEach, String afterEach)
  8. implodeArray(Object[] array)
  9. implodeArray(Object[] items, String prefix, String suffix, String delimiter, String lastItemSuffix)