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

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

Description

Perform reverse of split on a list.

License

Apache License

Parameter

Parameter Description
array Members to be joined.
sep Separator between individual members of the array.

Return

String representation of all members, delimited by the separator.

Declaration

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

Method Source Code


//package com.java2s;
/*/*from w w  w.j av  a2s.c  o  m*/
 * Copyright 2015 Charles University in Prague
 * Copyright 2015 Vojtech Horky
 * 
 * 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 {
    /** Perform reverse of split on a list.
     * 
     * @param list Members to be joined.
     * @return String representation of all members, connected by comma.
     */
    public static String join(List<?> list) {
        if (list.isEmpty()) {
            return "";
        }
        StringBuilder result = new StringBuilder();
        boolean afterFirst = false;
        for (Object o : list) {
            if (afterFirst) {
                result.append(',');
            }
            result.append(o.toString());
            afterFirst = true;
        }
        return result.toString();
    }

    /** Perform reverse of split on a list.
     * 
     * @param array Members to be joined.
     * @param sep Separator between individual members of the array.
     * @return String representation of all members, delimited by the separator.
     */
    public static String join(Object[] array, String sep) {
        if (array.length == 0) {
            return "";
        }
        StringBuilder result = new StringBuilder();
        boolean afterFirst = false;
        for (Object o : array) {
            if (afterFirst) {
                result.append(sep);
            }
            result.append(o.toString());
            afterFirst = true;
        }
        return result.toString();
    }

    /** Perform revers of split on all arguments.
     * 
     * @param objects Objects to be joined.
     * @return String representation of all objects, connected by comma.
     */
    public static String join(Object... objects) {
        return join(objects, ",");
    }
}

Related

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