Java Array Implode implode(String[] inputArray, String glueString)

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

Description

Method to join array elements of type string

License

MIT 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 implode(String[] inputArray, String glueString) 

Method Source Code

//package com.java2s;
/*/*from  w ww  .  j  a v  a 2 s.  c o m*/
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

import java.util.*;

public class Main {
    public static final String EMPTY_STRING = "";

    /**
     * Method to join array elements of type string
     *
     * @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 implode(String[] inputArray, String glueString) {
        String output = EMPTY_STRING;

        if (inputArray != null && inputArray.length > 0) {
            StringBuilder sb = new StringBuilder();
            sb.append(inputArray[0]);

            for (int i = 1; i < inputArray.length; i++) {
                sb.append(glueString);
                sb.append(inputArray[i]);
            }

            output = sb.toString();
        }

        return output;
    }

    /**
     * Method to join a list of elements of type string
     *
     * @param inputList  List which contains strings
     * @param glueString String between each array element
     * @return String containing all array elements separated by glue string.
     */
    public static String implode(List inputList, String glueString) {
        String output = EMPTY_STRING;

        if (inputList != null && !inputList.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            sb.append(inputList.get(0));

            for (int i = 1; i < inputList.size(); i++) {
                sb.append(glueString);
                sb.append(inputList.get(i));
            }

            output = sb.toString();
        }

        return output;
    }
}

Related

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