Java String Array Merge mergeJoin(String sep, String[] a1, String[] a2)

Here you can find the source of mergeJoin(String sep, String[] a1, String[] a2)

Description

merge Join

License

Apache License

Declaration

public static String mergeJoin(String sep, String[] a1, String[] a2) 

Method Source Code

//package com.java2s;
//  Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    public static String mergeJoin(String sep, String[] a1, String[] a2) {
        if (a1.length != a2.length) {
            System.out.println("Unable to merge String arrays of different length!");
            return join(a1);
        }//from  w w w  .  jav  a 2 s  .c om

        StringBuilder sb = new StringBuilder(a1[0]);
        sb.append(sep).append(a2[0]);
        for (int i = 1; i < a1.length; i++) {
            sb.append(" ").append(a1[i]).append(sep).append(a2[i]);
        }
        return sb.toString();
    }

    public static String join(Object[] array) {
        return join(" ", array);
    }

    public static String join(String sep, Object[] array) {
        StringBuilder sb = new StringBuilder(array[0].toString());
        for (int i = 1; i < array.length; i++) {
            sb.append(sep).append(array[i].toString());
        }
        return sb.toString();
    }

    public static String join(String[] array) {
        return join(" ", array);
    }

    public static String join(String sep, String[] array) {
        StringBuilder sb = new StringBuilder(array[0]);
        for (int i = 1; i < array.length; i++) {
            sb.append(sep).append(array[i]);
        }
        return sb.toString();
    }

    public static String join(int[] array) {
        return join(" ", array);
    }

    public static String join(String sep, int[] array) {
        StringBuilder sb = new StringBuilder(Integer.toString(array[0]));
        for (int i = 1; i < array.length; i++) {
            sb.append(sep).append(array[i]);
        }
        return sb.toString();
    }

    public static String join(double[] array) {
        return join(" ", array);
    }

    public static String join(String sep, double[] array) {
        StringBuilder sb = new StringBuilder(Double.toString(array[0]));
        for (int i = 1; i < array.length; i++) {
            sb.append(sep).append(array[i]);
        }
        return sb.toString();
    }

    /**
     * <p>Joins the elements of the provided array into a single String
     * containing the provided list of elements.</p>
     *
     * <p>No delimiter is added before or after the list.
     * A <code>null</code> separator is the same as an empty String ("").
     * Null objects or empty strings within the array are represented by
     * empty strings.</p>
     *
     * <pre>
     * StringUtils.join(null, *)                = null
     * StringUtils.join([], *)                  = ""
     * StringUtils.join([null], *)              = ""
     * StringUtils.join(["a", "b", "c"], "--")  = "a--b--c"
     * StringUtils.join(["a", "b", "c"], null)  = "abc"
     * StringUtils.join(["a", "b", "c"], "")    = "abc"
     * StringUtils.join([null, "", "a"], ',')   = ",,a"
     * </pre>
     *
     * @param array  the array of values to join together, may be null
     * @param separator  the separator character to use, null treated as ""
     * @param startIndex the first index to start joining from.  It is
     * an error to pass in an end index past the end of the array
     * @param endIndex the index to stop joining from (exclusive). It is
     * an error to pass in an end index past the end of the array
     * @return the joined String, <code>null</code> if null array input
     */
    public static String join(Object[] array, String separator, int startIndex, int endIndex) {
        if (array == null) {
            return null;
        }
        if (separator == null) {
            separator = "";
        }

        // endIndex - startIndex > 0:   Len = NofStrings *(len(firstString) + len(separator))
        //           (Assuming that all Strings are roughly equally long)
        int bufSize = (endIndex - startIndex);
        if (bufSize <= 0) {
            return "";
        }

        bufSize *= ((array[startIndex] == null ? 16 : array[startIndex].toString().length()) + separator.length());

        StringBuffer buf = new StringBuffer(bufSize);

        for (int i = startIndex; i < endIndex; i++) {
            if (i > startIndex) {
                buf.append(separator);
            }
            if (array[i] != null) {
                buf.append(array[i]);
            }
        }
        return buf.toString();
    }

    /**
     * <p>Joins the elements of the provided array into a single String
     * containing the provided list of elements.</p>
     *
     * <p>No delimiter is added before or after the list.
     * A <code>null</code> separator is the same as an empty String ("").
     * Null objects or empty strings within the array are represented by
     * empty strings.</p>
     *
     * <pre>
     * StringUtils.join(null, *)                = null
     * StringUtils.join([], *)                  = ""
     * StringUtils.join([null], *)              = ""
     * StringUtils.join(["a", "b", "c"], "--")  = "a--b--c"
     * StringUtils.join(["a", "b", "c"], null)  = "abc"
     * StringUtils.join(["a", "b", "c"], "")    = "abc"
     * StringUtils.join([null, "", "a"], ',')   = ",,a"
     * </pre>
     *
     * @param array  the array of values to join together, may be null
     * @param separator  the separator character to use, null treated as ""
     * @param startIndex the first index to start joining from.  It is
     * an error to pass in an end index past the end of the array
     * @param endIndex the index to stop joining from (exclusive). It is
     * an error to pass in an end index past the end of the array
     * @param internalSep separator for each element in array. each element
     * will be split with the separator and the first element will be
     * added to the buffer
     * @return the joined String, <code>null</code> if null array input
     */
    public static String join(Object[] array, String separator, int startIndex, int endIndex, String internalSep) {
        if (array == null) {
            return null;
        }
        if (separator == null) {
            separator = "";
        }

        // endIndex - startIndex > 0:   Len = NofStrings *(len(firstString) + len(separator))
        //           (Assuming that all Strings are roughly equally long)
        int bufSize = (endIndex - startIndex);
        if (bufSize <= 0) {
            return "";
        }

        bufSize *= ((array[startIndex] == null ? 16 : array[startIndex].toString().length()) + separator.length());

        StringBuffer buf = new StringBuffer(bufSize);

        for (int i = startIndex; i < endIndex; i++) {
            if (i > startIndex) {
                buf.append(separator);
            }
            if (array[i] != null) {
                String token = array[i].toString().split(internalSep)[0];
                buf.append(token);
            }
        }
        return buf.toString();
    }
}

Related

  1. merge(String[] pieces, String sep)
  2. merge(String[] src, String delimiter)
  3. merge(String[] str, String sep)
  4. merge(T[] arr0, String delimiter)
  5. mergeInts(int[] values, String delimiter)
  6. mergeJoin(String sep, String[] a1, String[] a2)
  7. mergeString(String currentValue, String newValue)
  8. mergeString(String[] str1, String[] str2)
  9. mergeStrings(Object... strings)