Java Array Join join(Object[] array, String separator)

Here you can find the source of join(Object[] array, String separator)

Description

join array of strings by separator

License

Apache License

Parameter

Parameter Description
array of strings
separator a parameter

Return

joined string

Declaration

public static String join(Object[] array, String separator) 

Method Source Code

//package com.java2s;
/*/*w  w w  .  ja va2 s .  co  m*/
 * 
    
 StringUtil.java
    
 Copyright 2004-2007 KUBO Hiroya (hiroya@cuc.ac.jp).
    
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import java.util.List;

public class Main {
    /**
     * join list of strings by separator
     * 
     * @param stringList
     *            list of strings
     * @param separator
     *            null is accepted (treated same as "").
     * @return joined string
     */
    public static String join(List<String> stringList, String separator) {
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < stringList.size(); i++) {
            if (i != 0) {
                if (separator != null) {
                    ret.append(separator);
                }
            }
            ret.append(stringList.get(i));
        }
        return ret.toString();
    }

    /**
     * join array of strings by separator
     * 
     * @param array
     *            of strings
     * @param separator
     * @return joined string
     */
    public static String join(Object[] array, String separator) {
        StringBuilder ret = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0) {
                if (separator != null) {
                    ret.append(separator);
                }
            }
            ret.append(array[i]);
        }
        return ret.toString();
    }
}

Related

  1. join(Object[] array, String glue)
  2. join(Object[] array, String linker)
  3. join(Object[] array, String sep)
  4. join(Object[] array, String separator)
  5. join(Object[] array, String separator)
  6. join(Object[] array, String separator)
  7. join(Object[] array, String separator)
  8. join(Object[] array, String separator)
  9. join(Object[] array, String separator)