Java List Combine combineString(List lstInput, String strToken)

Here you can find the source of combineString(List lstInput, String strToken)

Description

Combine string array as string, split by token string

License

Open Source License

Parameter

Parameter Description
lstInput list which includes elements to be combined
strToken split token

Return

string combined with strsInput, splited by strToken

Declaration

public static String combineString(List lstInput, String strToken) 

Method Source Code

//package com.java2s;
/*//from   w w  w  .  j  ava 2 s  . c  o  m
 * File: $RCSfile$
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */

import java.util.Iterator;

import java.util.List;

public class Main {
    /**
     * Combine string array as string, split by token string
     *
     * @param strsInput Object array which includes elements to be combined
     * @param strToken  split token
     * @return string combined with <code>strsInput</code>, splited by <code>strToken</code>
     */
    public static String combineString(Object[] strsInput, String strToken) {
        if (strsInput == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        int l = strsInput.length;
        for (int i = 0; i < l; i++) {
            sb.append(strsInput[i].toString());
            if (i < l - 1) {
                sb.append(strToken);
            }
        }
        return sb.toString();
    }

    /**
     * Combine int array as string, split by token string
     *
     * @param strsInput Object array which includes elements to be combined
     * @param strToken  split token
     * @return string combined with <code>strsInput</code>, splited by <code>strToken</code>
     */
    public static String combineString(int[] strsInput, String strToken) {
        if (strsInput == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        int l = strsInput.length;
        for (int i = 0; i < l; i++) {
            sb.append(strsInput[i]);
            if (i < l - 1) {
                sb.append(strToken);
            }
        }
        return sb.toString();
    }

    /**
     * Combine string array as string, split by token string
     *
     * @param lstInput list which includes elements to be combined
     * @param strToken split token
     * @return string combined with <code>strsInput</code>, splited by <code>strToken</code>
     */
    public static String combineString(List lstInput, String strToken) {
        if (lstInput == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        for (Iterator iterator = lstInput.iterator(); iterator.hasNext();) {
            sb.append(iterator.next().toString());
            if (iterator.hasNext()) {
                sb.append(strToken);
            }
        }
        return sb.toString();
    }

    /**
     * Split string to several segements with specified length, split by token string
     *
     * @param strItem  string to be splited
     * @param strToken split token
     * @param itemLen  specified length
     * @return string splited to several segements with <code>itemLen</code>, splited by <code>strToken</code>
     */
    public static String combineString(String strItem, String strToken, int itemLen) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < itemLen; i++) {
            if (i > 0) {
                sb.append(strToken);
            }
            sb.append(strItem);
        }
        return sb.toString();
    }

    /**
     * Convert object to string, if object is null, return "".
     *
     * @param object object converted to string
     * @return if object is null, return "". else object.toString();
     */
    public static String toString(Object object) {
        if (object == null) {
            return null;
        } else {
            return object.toString();
        }
    }
}

Related

  1. combineFloat(List nums)
  2. combineLines(List lines)
  3. combineList(List l1, List l2)
  4. combineLists(List keys, List values)
  5. combineLists(List a, List b)
  6. combineStringList(List sList)
  7. combineSubtractArrays(List a1, List a2)
  8. getCombinedColumnName(final List columnNames, final boolean sortByName)
  9. putCombinedList(Map map, Object key, Object value)