concat By CSV - Android java.io

Android examples for java.io:CSV

Description

concat By CSV

Demo Code

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static String concatByCSV(String[] strs) {
        StringBuffer value = new StringBuffer();
        for (int i = 0; i < strs.length; i++) {
            String filterValue = new String(strs[i]);
            if (filterValue.indexOf(",") != -1) {
                if (filterValue.indexOf("\"") != -1) {
                    filterValue = filterValue.replaceAll("\"", "\"\"");
                }/*from ww  w .  ja  va2 s.  com*/
                StringBuffer temp = new StringBuffer();
                temp.append("\"");
                temp.append(filterValue);
                temp.append("\"");
                filterValue = temp.toString();
            } else if (filterValue.indexOf("\"") != -1) {
                filterValue = filterValue.replaceAll("\"", "\"\"");
                StringBuffer temp = new StringBuffer();
                temp.append("\"");
                temp.append(filterValue);
                temp.append("\"");
                filterValue = temp.toString();
            }
            value.append(filterValue);
            if (i != strs.length - 1) {
                value.append(",");
            }
        }
        return value.toString();
    }
    public static String concatByCSV(List<String> strList) {
        String[] strArr = new String[strList.size()];
        return concatByCSV(strList.toArray(strArr));
    }

}

Related Tutorials