Here you can find the source of join(String[] inputArray, String glueString)
Parameter | Description |
---|---|
inputArray | Array which contains strings |
glueString | String between each array element |
public static String join(String[] inputArray, String glueString)
//package com.java2s; //License from project: Creative Commons License import java.util.Iterator; import java.util.List; public class Main { /**// w w w. j ava2 s . c o m * Joins an arraylist together by a delimiter * @param s * @param delimiter * @return */ public static String join(List<String> s, String delimiter) { StringBuffer buffer = new StringBuffer(); Iterator<?> iter = s.iterator(); while (iter.hasNext()) { buffer.append(iter.next()); if (iter.hasNext()) buffer.append(delimiter); } return buffer.toString(); } /** * Method to join array elements of type string * @author Hendrik Will, imwill.com, bug fixes by viveleroi * @param inputArray Array which contains strings * @param glueString String between each array element * @return String containing all array elements separated by glue string */ public static String join(String[] inputArray, String glueString) { String output = ""; if (inputArray.length > 0) { StringBuilder sb = new StringBuilder(); if (!inputArray[0].isEmpty()) { sb.append(inputArray[0]); } for (int i = 1; i < inputArray.length; i++) { if (!inputArray[i].isEmpty()) { if (sb.length() > 0) { sb.append(glueString); } sb.append(inputArray[i]); } } output = sb.toString(); } return output; } }