package jp.hasc.hasctool.core.util;

import java.util.ArrayList;

/**
 * @author iwasaki
 * todo: escape CSV field
 */
public class CSVUtil {
	
	public static String toCSVRecord(String... fields) {
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<fields.length;++i) {
			if (i>0) sb.append(",");
			sb.append(fields[i]);
		}
		return sb.toString();
	}
	
	public static String[] parseCSVRecord(String row) {
		ArrayList<String> ss=new ArrayList<String>();
		int i=0;
		while(i<row.length()) {
			int idx=row.indexOf(',',i);
			if (idx<0) break;
			ss.add(row.substring(i,idx));
			i=idx+1;
		}
		ss.add(row.substring(i,row.length()));
		return ss.toArray(new String[ss.size()]);
	}

}
