Java Array Join join(String sep, String[] arr)

Here you can find the source of join(String sep, String[] arr)

Description

Join an array of Strings using given separator

License

Open Source License

Parameter

Parameter Description
sep separator
arr array to be join

Return

joined String, or null if arr is null

Declaration

public static String join(String sep, String[] arr) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *     This file is part of AlignerBoost, a generalized software toolkit to boost
 *     the NextGen sequencing (NGS) aligner precision and sensitivity.
 *     Copyright (C) 2015  Qi Zheng/*from   w  ww. j  a v a 2  s .c  o m*/
 *
 *     AlignerBoost 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.
 *
 *     AlignerBoost 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 AlignerBoost.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import java.util.Map;

public class Main {
    /**
     * Join an array of Strings using given separator
     * @param sep  separator
     * @param arr  array to be join
     * @return  joined String, or null if arr is null
     */
    public static String join(String sep, String[] arr) {
        if (arr == null)
            return null;
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < arr.length; i++)
            str.append(i == 0 ? arr[i] : sep + arr[i]);
        return str.toString();
    }

    /**
     * Join an array of Strings using space
     * @param arr  array to be join
     * @return  joined String, or null if arr is null
     */
    public static String join(String[] arr) {
        return join(" ", arr);
    }

    /**
     * A generic method to join a Map of any objects using : and given separator
     * @param  map any map to be joined to string
     * @param  sep String separator
     * @return  joined string as "key:value[<sep>key:value]", or null if map is null
     */
    public static <K, V> String join(Map<K, V> map, String sep) {
        if (map == null)
            return null;
        StringBuilder str = new StringBuilder();
        for (Map.Entry<K, V> entry : map.entrySet())
            str.append(str.length() == 0 ? entry.getKey() + ":" + entry.getValue()
                    : sep + entry.getKey() + ":" + entry.getValue());
        return str.toString();
    }

    /**
     * A generic method to join a Map of any objects using : and default separator ','
     * @param  map any map to be joined to string
     * @return  joined string as "key:value[,key:value]", or null if map is null
     */
    public static <K, V> String join(Map<K, V> map) {
        return join(map, ",");
    }
}

Related

  1. join(String s, String[] arr, int off, int end)
  2. join(String s[], String glue)
  3. join(String sep, final String[] strings)
  4. join(String sep, Object[] os)
  5. join(String sep, String[] a)
  6. join(String separator, String... array)
  7. join(String separator, String[] strings)
  8. join(String separator, String[] strings)
  9. join(String[] a, String delimiter, Integer startIndex)