Java tutorial
//package com.java2s; //License from project: Open Source License import java.net.URLEncoder; import java.util.*; public class Main { /** * Used for flattening a collection of objects into a string * @param array Array of elements to flatten * @param fmt Format string to use for array flattening * @param separator Separator to use for string concat * @return Representative string made up of array elements */ private static String flattenCollection(String elemName, Collection<?> array, String fmt, char separator) { StringBuilder builder = new StringBuilder(); //append all elements in the array into a string for (Object element : array) { String elemValue = null; //replace null values with empty string to maintain index order if (null == element) { elemValue = ""; builder.append(String.format(fmt, elemName, elemValue, separator)); } else { elemValue = tryUrlEncode(element.toString()); builder.append(String.format(fmt, elemName, elemValue, separator)); } } //remove the last separator, if appended if ((builder.length() > 1) && (builder.charAt(builder.length() - 1) == separator)) builder.deleteCharAt(builder.length() - 1); return builder.toString(); } /** * Tries Url encode using UTF-8 * @param value The value to url encode * @return */ private static String tryUrlEncode(String value) { try { return URLEncoder.encode(value, "UTF-8"); } catch (Exception ex) { return value; } } }