Combine a list to a string of the list's meta with a separator - Android java.util

Android examples for java.util:List

Description

Combine a list to a string of the list's meta with a separator

Demo Code

import java.io.UnsupportedEncodingException;
import java.util.*;

public class Main{

    /**//ww  w .  j  a  v a 2 s  .c o  m
     * Combine a list to a string of the list's meta with a separator
     * if the element is a string then surround with "'".
     * It is frequently used in sql statement!
     *
     * @param list      <code>List</code>
     * @param separator specified separator
     * @return converted string with <code>separator</code>
     */
    public static String combineStringWithQuotation(List list,
            String separator) {
        StringBuffer sb = new StringBuffer("");
        if (list != null && list.size() > 0) {
            for (Object item : list) {
                if (sb.length() != 0) {
                    sb.append(separator);
                }
                if (item instanceof String) {
                    String strItem = (String) item;
                    sb.append("'");
                    sb.append(strItem);
                    sb.append("'");
                } else {
                    sb.append(item);
                }
            }
        }
        return sb.toString();
    }

}

Related Tutorials