Java List to String convertListToString(List itemList, String separator)

Here you can find the source of convertListToString(List itemList, String separator)

Description

convert List To String

License

Open Source License

Declaration

public static String convertListToString(List<String> itemList, String separator) 

Method Source Code

//package com.java2s;
/*/*from  w ww  .ja  va2s  . c o m*/
 * Copyright (C) 2006-2016 Talend Inc. - www.talend.com
 * 
 * This source code is available under agreement available at
 * %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt
 * 
 * You should have received a copy of the agreement along with this program; if not, write to Talend SA 9 rue Pages
 * 92150 Suresnes, France
 */

import java.util.List;

public class Main {
    public static String convertListToString(List<String> itemList, String separator) {
        if (itemList == null) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < itemList.size(); i++) {
            result.append((i > 0) ? separator : ""); //$NON-NLS-1$ 
            result.append(escape(itemList.get(i)));
        }
        return result.toString();
    }

    public static String convertListToString(List<String> itemList) {
        if (itemList == null) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < itemList.size(); i++) {
            result.append((i > 0) ? ";" : ""); //$NON-NLS-1$ //$NON-NLS-2$ 
            result.append(escapeSemicolon(itemList.get(i)));
        }
        return result.toString();
    }

    public static String escape(String src) {
        int i;
        char j;
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length() * 6);

        for (i = 0; i < src.length(); i++) {

            j = src.charAt(i);

            if (Character.isDigit(j) || Character.isLowerCase(j) || Character.isUpperCase(j)) {
                tmp.append(j);
            } else if (j < 256) {
                tmp.append("%"); //$NON-NLS-1$
                if (j < 16) {
                    tmp.append("0"); //$NON-NLS-1$
                }
                tmp.append(Integer.toString(j, 16));
            } else {
                tmp.append("%u"); //$NON-NLS-1$
                tmp.append(Integer.toString(j, 16));
            }
        }
        return tmp.toString();
    }

    public static String escapeSemicolon(String src) {
        int i;
        char j;
        StringBuffer tmp = new StringBuffer();
        tmp.ensureCapacity(src.length() * 6);
        for (i = 0; i < src.length(); i++) {
            j = src.charAt(i);
            if (';' == j || '%' == j) {
                if (j < 256) {
                    tmp.append("%"); //$NON-NLS-1$
                    if (j < 16) {
                        tmp.append("0"); //$NON-NLS-1$
                    }
                    tmp.append(Integer.toString(j, 16));
                } else {
                    tmp.append("%u"); //$NON-NLS-1$
                    tmp.append(Integer.toString(j, 16));
                }
            } else {
                tmp.append(j);
            }
        }
        return tmp.toString();
    }
}

Related

  1. convertListToString(List list, String delimiter, char encloser)
  2. convertListToString(List list)
  3. convertListToString(List data)
  4. convertListToString(List list, char delimiter, int estimatedLength)
  5. convertListToString(List dests)
  6. convertListToString(List listStr, String separator)
  7. convertListToString(List strings)
  8. convertListToString(List strList)
  9. convertListToString(List valueList)

  10. HOME | Copyright © www.java2s.com 2016