Java Array Flatten flattenArray(Object[] words)

Here you can find the source of flattenArray(Object[] words)

Description

Converts object array to a big string separated by comma.

License

LGPL

Parameter

Parameter Description
words a parameter

Return

a flattened string

Declaration

public static String flattenArray(Object[] words) 

Method Source Code

//package com.java2s;
/*//w w  w . ja  va 2s.c  om
 *   This software is distributed under the terms of the FSF 
 *   Gnu Lesser General Public License (see lgpl.txt). 
 *
 *   This program is distributed WITHOUT ANY WARRANTY. See the
 *   GNU General Public License for more details.
 */

public class Main {
    /**
     * Converts object array to a big string separated by comma.
     * 
     * @param words
     * @return a flattened string
     */
    public static String flattenArray(Object[] words) {
        return flattenArray(words, ",");
    }

    /**
     * Converts object array to a big string separated by splitter.
     * 
     * @param words
     * @param splitter
     * @return a flattened string
     */
    public static String flattenArray(Object[] words, String splitter) {
        return flattenArray("", words, splitter);
    }

    /**
     * Converts object array to a big string separated by splitter.
     * 
     * @param prefix prefix before each element
     * @param words an array of elements
     * @param splitter a string which separates each element
     * @return a flattened string
     */
    public static String flattenArray(String prefix, Object[] words,
            String splitter) {
        if (words == null)
            return null;

        StringBuilder sb = new StringBuilder("");
        int length = words.length;
        for (int i = 0; i < length; i++) {
            sb.append(prefix).append(words[i]).append(splitter);
        }
        return removeLastToken(sb.toString(), splitter);
    }

    public static StringBuffer removeLastToken(StringBuffer word,
            String token) {
        if (word == null)
            return word;

        String newWord = removeLastToken(word.toString(), token);
        return new StringBuffer(newWord);
    }

    public static StringBuilder removeLastToken(StringBuilder word,
            String token) {
        if (word == null)
            return word;

        String newWord = removeLastToken(word.toString(), token);
        return new StringBuilder(newWord);
    }

    public static String removeLastToken(String word, String token) {
        return (word == null || token == null || "".equals(token) || !word
                .endsWith(token)) ? word : word.substring(0, word.length()
                - token.length());
    }
}

Related

  1. flatten(Object[] lines, String sep)
  2. flatten(String s[])
  3. flatten(String[] strings, String separator)
  4. flattenArguments(String[] arguments)
  5. flattenArray(Object... objects)
  6. flattenArray(String[] aString)
  7. flattenBytes(byte[] b)
  8. flattenIndices(int[] indices, int[] extents)
  9. flattenNTriples(final String[] values)