Jul 092014
 

Here’s a short method to add a delimiter to a list of strings

        /**
	 * Creates a delimited list
	 * @param list The list of strings to delimit
	 * @param delimiter The string to use as a delimiter
	 * @return A delimited list
	 */
	public staatic String delimitString(final List list, String delimiter) {
		
        StringBuilder result = new StringBuilder();
        boolean addComma = false;

        for (String element : list) {

            // don't add a comma before the first
            if (addComma) {
                result.append(delimiter);
            }

            // after the first iteration, it's okay to add a comma
            addComma = true;

            result.append(element);
        }
        
        return result.toString();
    }
 Posted by at 4:01 pm