Java Array Join join(String sep, final String[] strings)

Here you can find the source of join(String sep, final String[] strings)

Description

join

License

Open Source License

Parameter

Parameter Description
sep a parameter
strings a parameter

Declaration

public static final String join(String sep, final String[] strings) 

Method Source Code

//package com.java2s;

import java.util.List;

public class Main {
    public static final String STR_EMP = "";

    /**/*from  www  .  j a  va  2  s  .co  m*/
     * 
     * @param sep
     * @param strings
     * @return
     */
    public static final String join(String sep, final String[] strings) {
        if (strings == null) {
            return null;
        }
        final int length = strings.length;
        if (length == 0) {
            return STR_EMP;
        }
        final int sepLen;
        if (sep == null) {
            sepLen = 4;
            sep = "null";
        } else {
            sepLen = sep.length();
        }
        int intTmp = strings[length - 1] == null ? 4 : strings[length - 1].length();
        for (int i = length - 2; i >= 0; i--) {
            intTmp += sepLen + (strings[i] == null ? 4 : strings[i].length());
        }
        final char[] charsArray = new char[intTmp];
        if (strings[0] == null) {
            charsArray[0] = 'n';
            charsArray[1] = 'u';
            charsArray[2] = 'l';
            charsArray[3] = 'l';
            intTmp = 4;
        } else if (strings[0].length() != 0) {
            strings[0].getChars(0, strings[0].length(), charsArray, 0);
            intTmp += strings[0].length();
        }
        for (int i = 1; i < length; i++) {
            sep.getChars(0, sepLen, charsArray, intTmp);
            intTmp += sepLen;
            if (strings[0] == null) {
                charsArray[intTmp + 1] = 'n';
                charsArray[intTmp + 2] = 'u';
                charsArray[intTmp + 3] = 'l';
                charsArray[intTmp + 4] = 'l';
                intTmp += 4;
            } else if (strings[i].length() != 0) {
                strings[i].getChars(0, strings[i].length(), charsArray, intTmp);
                intTmp += strings[i].length();
            }
        }
        return new String(charsArray);
    }

    /**
     * 
     * @param sep
     * @param objects
     * @return 
     */
    public static final String join(String sep, final Object[] objects) {
        if (objects == null) {
            return null;
        }
        final int length = objects.length;
        if (length == 0) {
            return STR_EMP;
        }
        final int sepLen;
        if (sep == null) {
            sepLen = 4;
            sep = "null";
        } else {
            sepLen = sep.length();
        }
        final String[] strings = new String[length];
        strings[length - 1] = objects[length - 1] == null ? "null" : objects[length - 1].toString();
        int intTmp = strings[length - 1].length();
        for (int i = length - 2; i >= 0; i--) {
            strings[i] = objects[i] == null ? "null" : objects[i].toString();
            intTmp += sepLen + strings[i].length();
        }
        final char[] charsArray = new char[intTmp];
        strings[0].getChars(0, strings[0].length(), charsArray, 0);
        intTmp = strings[0].length();
        for (int i = 1; i < length; i++) {
            sep.getChars(0, sepLen, charsArray, intTmp);
            intTmp += sepLen;
            strings[i].getChars(0, strings[i].length(), charsArray, intTmp);
            intTmp += strings[i].length();
        }
        return new String(charsArray);

    }

    /**
     * 
     * @param sep
     * @param strings
     * @return 
     */
    public static final String join(final char sep, final String[] strings) {
        if (strings == null) {
            return null;
        }
        final int length = strings.length;
        if (length == 0) {
            return STR_EMP;
        }
        int intTmp = strings[length - 1] == null ? 4 : strings[length - 1].length();
        for (int i = length - 2; i >= 0; i--) {
            intTmp += 1 + (strings[i] == null ? 4 : strings[i].length());
        }
        final char[] charsArray = new char[intTmp];
        if (strings[0] == null) {
            charsArray[0] = 'n';
            charsArray[1] = 'u';
            charsArray[2] = 'l';
            charsArray[3] = 'l';
            intTmp = 4;
        } else if (strings[0].length() != 0) {
            strings[0].getChars(0, strings[0].length(), charsArray, 0);
            intTmp += strings[0].length();
        }
        for (int i = 1; i < length; i++) {
            charsArray[intTmp] = sep;
            intTmp += 1;
            if (strings[0] == null) {
                charsArray[intTmp + 1] = 'n';
                charsArray[intTmp + 2] = 'u';
                charsArray[intTmp + 3] = 'l';
                charsArray[intTmp + 4] = 'l';
                intTmp += 4;
            } else if (strings[i].length() != 0) {
                strings[i].getChars(0, strings[i].length(), charsArray, intTmp);
                intTmp += strings[i].length();
            }
        }
        return new String(charsArray);
    }

    /**
     * 
     * @param sep
     * @param objects
     * @return 
     */
    public static final String join(char sep, final Object[] objects) {
        if (objects == null) {
            return null;
        }
        final int length = objects.length;
        if (length == 0) {
            return STR_EMP;
        }
        final String[] strings = new String[length];
        strings[length - 1] = objects[length - 1] == null ? "null" : objects[length - 1].toString();
        int intTmp = strings[length - 1].length();
        for (int i = length - 2; i >= 0; i--) {
            strings[i] = objects[i] == null ? "null" : objects[i].toString();
            intTmp += 1 + strings[i].length();
        }
        final char[] charsArray = new char[intTmp];
        strings[0].getChars(0, strings[0].length(), charsArray, 0);
        intTmp = strings[0].length();
        for (int i = 1; i < length; i++) {
            charsArray[intTmp] = sep;
            intTmp += 1;
            strings[i].getChars(0, strings[i].length(), charsArray, intTmp);
            intTmp += strings[i].length();
        }
        return new String(charsArray);

    }

    /**
     * 
     * @param sep
     * @param it
     * @return
     */
    public static String join(String sep, final List list) {
        if (list == null) {
            return null;
        } else if (list.isEmpty()) {
            return STR_EMP;
        }
        final int length = list.size();
        final int sepLen;
        if (sep == null) {
            sepLen = 4;
            sep = "null";
        } else {
            sepLen = sep.length();
        }
        final String[] strings = new String[length];
        int intTmp = length - 1;
        strings[intTmp] = list.get(intTmp) == null ? "null" : list.get(intTmp).toString();
        intTmp = strings[intTmp].length();

        for (int i = length - 2; i >= 0; i--) {
            strings[i] = list.get(i) == null ? "null" : list.get(i).toString();
            intTmp += sepLen + strings[i].length();
        }
        final char[] charsArray = new char[intTmp];
        strings[0].getChars(0, strings[0].length(), charsArray, 0);
        intTmp = strings[0].length();
        for (int i = 1; i < length; i++) {
            sep.getChars(0, sepLen, charsArray, intTmp);
            intTmp += sepLen;
            strings[i].getChars(0, strings[i].length(), charsArray, intTmp);
            intTmp += strings[i].length();
        }
        return new String(charsArray);
    }

    /**
     * 
     * @param sep
     * @param list
     * @return 
     */
    public static String join(char sep, final List list) {
        if (list == null) {
            return null;
        } else if (list.isEmpty()) {
            return STR_EMP;
        }
        final int length = list.size();
        final String[] strings = new String[length];
        int intTmp = length - 1;
        strings[intTmp] = list.get(intTmp) == null ? "null" : list.get(intTmp).toString();
        intTmp = strings[intTmp].length();

        for (int i = length - 2; i >= 0; i--) {
            strings[i] = list.get(i) == null ? "null" : list.get(i).toString();
            intTmp += 1 + strings[i].length();
        }
        final char[] charsArray = new char[intTmp];
        strings[0].getChars(0, strings[0].length(), charsArray, 0);
        intTmp = strings[0].length();
        for (int i = 1; i < length; i++) {
            charsArray[intTmp] = sep;
            intTmp += 1;
            strings[i].getChars(0, strings[i].length(), charsArray, intTmp);
            intTmp += strings[i].length();
        }
        return new String(charsArray);
    }

    /**
     * 
     * @param objs
     * @return
     */
    public static String toString(final Object[] objs) {
        if (objs == null) {
            return STR_EMP;
        }
        final int len = objs.length;
        if (len == 0) {
            return STR_EMP;
        }
        final StringBuilder buf = new StringBuilder(len * 12);
        for (int i = 0; i < len - 1; i++) {
            buf.append(objs[i]).append(", ");
        }
        return buf.append(objs[len - 1]).toString();
    }

    /**
     * 
     * @param s
     * @return
     */
    public static final boolean isEmpty(final String s) {
        return (s == null || s.length() == 0);
    }
}

Related

  1. join(String delimiter, String[] s)
  2. join(String glue, String[] strings)
  3. join(String glue, String[] strings)
  4. join(String s, String[] arr, int off, int end)
  5. join(String s[], String glue)
  6. join(String sep, Object[] os)
  7. join(String sep, String[] a)
  8. join(String sep, String[] arr)
  9. join(String separator, String... array)