Java Array Join join(String[] inputArray, String glueString)

Here you can find the source of join(String[] inputArray, String glueString)

Description

Method to join array elements of type string

License

Creative Commons License

Parameter

Parameter Description
inputArray Array which contains strings
glueString String between each array element

Return

String containing all array elements separated by glue string

Declaration

public static String join(String[] inputArray, String glueString) 

Method Source Code

//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;
    }
}

Related

  1. join(String[] array, String separator)
  2. join(String[] arrayOfString)
  3. join(String[] datalines)
  4. join(String[] elements, String separator)
  5. join(String[] elements, String separator)
  6. join(String[] items, String delimiter)
  7. join(String[] lines, String delim)
  8. join(String[] lines, String sep)
  9. join(String[] lines, String separator)