Java Array Join join(String[] strings, String glue)

Here you can find the source of join(String[] strings, String glue)

Description

Joins a String[] using glue to separate the elements of the array.

License

Open Source License

Parameter

Parameter Description
strings an array of String s to be joined together
glue the String to use in between elements of the array

Return

a String of the combined contents of the array.

Declaration

public static String join(String[] strings, String glue) 

Method Source Code


//package com.java2s;
/*  /*from w  ww .  j a  v a 2  s  .c  om*/
 *  Copyright (C) 2011  Andrew Oberstar.  All rights reserved.
 *  DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.List;

public class Main {
    /**
     * Joins a {@code String[]} using {@code glue} to separate
     * the elements of the array.
     * @param strings an array of {@code String}s to be joined together
     * @param glue the {@code String} to use in between elements of the array 
     * @return a {@code String} of the combined contents of the array.
     */
    public static String join(String[] strings, String glue) {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < strings.length; i++) {
            builder.append(strings[i]);
            if (i != strings.length - 1) {
                builder.append(glue);
            }
        }
        return builder.toString();
    }

    /**
     * Joins a {@code List<String>} using {@code glue} to separate
     * the elements of the array.
     * 
     * @param strings an array of {@code String}s to be joined together
     * @param glue the {@code String} to use in between elements of the array 
     * @return a {@code String} of the combined contents of the array.
     */
    public static String join(List<String> strings, String glue) {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < strings.size(); i++) {
            builder.append(strings.get(i));
            if (i != strings.size() - 1) {
                builder.append(glue);
            }
        }
        return builder.toString();
    }
}

Related

  1. join(String[] stringArray, String separator)
  2. join(String[] strings)
  3. join(String[] strings, char delimiter)
  4. join(String[] strings, char delimiter)
  5. join(String[] strings, String glue)
  6. join(String[] strings, String separator)
  7. join(String[] strings, String separator)
  8. join(String[] strings, String seperator)
  9. join(String[] strParts, String glue)