Java Array Join join(final String[] arrays, final String seperator)

Here you can find the source of join(final String[] arrays, final String seperator)

Description

join

License

Apache License

Declaration

public static String join(final String[] arrays, final String seperator) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static String join(final String[] arrays, final String seperator) {

        final int len = arrays.length;
        if (arrays == null || len == 0) {
            return "";
        }//from  w w w  .j av a  2s.c o  m

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            sb.append(arrays[i]);

            if (seperator != null && (i < len - 1)) {
                sb.append(seperator);
            }
        }

        return sb.toString();

    }

    public static String join(final Collection<?> col, final String seperator) {

        final int size = col.size();
        if (col == null || size == 0) {
            return "";
        }

        StringBuilder sb = new StringBuilder();
        for (Iterator<?> itr = col.iterator(); itr.hasNext();) {
            final Object item = itr.next();
            sb.append(item.toString());

            if (seperator != null && itr.hasNext()) {
                sb.append(seperator);
            }
        }

        return sb.toString();

    }
}

Related

  1. join(CharSequence[] strings, CharSequence separator)
  2. join(final Object[] array)
  3. join(final Object[] array, final char separator)
  4. join(final Object[] array, final char separator)
  5. join(final String separator, final String[] array)
  6. join(final String[] in, final String sep)
  7. join(final String[] strings)
  8. join(final String[] strings, final String joinString)
  9. join(final String[] values, final String delimiter)