Here you can find the source of implode(String[] data, String glue)
Parameter | Description |
---|---|
data | string array |
glue | a parameter |
public static String implode(String[] data, String glue)
//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; } }