Java Array Join join(Object[] values, String join)

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

Description

Joins the values of the array together separated by the join argument.

License

Apache License

Declaration

public static String join(Object[] values, String join) 

Method Source Code


//package com.java2s;
/*//from  w  ww .jav  a2 s.  com
 * Copyright 2009 Bart Guijt and others.
 * 
 * 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 {
    /**
     * Joins the values of the array together separated by the join argument.
     */
    public static String join(Object[] values, String join) {
        if (values == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            if (i > 0) {
                sb.append(join);
            }
            sb.append(values[i]);
        }
        return sb.toString();
    }

    public static String join(List<?> values, String join) {
        if (values == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < values.size(); i++) {
            if (i > 0) {
                sb.append(join);
            }
            sb.append(values.get(i));
        }
        return sb.toString();
    }
}

Related

  1. join(Object[] objects)
  2. join(Object[] parts, String delim)
  3. join(Object[] strings, String delimiter)
  4. join(Object[] strings, String spliter)
  5. join(Object[] tokens, String delimiter)
  6. join(Object[]... arrays)
  7. join(String delimiter, Object[] strings)
  8. join(String delimiter, short[] array)
  9. join(String delimiter, String[] array)