Java tutorial
/* * Copyright 2012 Eng Kam Hon (kamhon@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.kamhon.ieagle.util; import java.util.Arrays; import java.util.Iterator; import java.util.List; import org.apache.commons.lang.StringUtils; public class StringUtil { private StringUtil() { } public static String convertToStringWithCommaNoSpacing(Object... objects) { return convertToStringWithSeparator(Arrays.asList(objects), ",", false); } public static String convertToStringWithComma(Object... objects) { return convertToStringWithComma(Arrays.asList(objects)); } public static String convertToStringWithComma(List<?> list) { return convertToStringWithSeparator(list, ",", true); } public static String convertToStringWithSeparator(List<?> list, String separator, boolean withSpacing) { String result = ""; if (CollectionUtil.isEmpty(list)) { return ""; } for (Iterator<?> iterator = list.iterator(); iterator.hasNext();) { Object o = iterator.next(); String s = o.toString(); result += s; if (iterator.hasNext()) { if (withSpacing) result += ", "; else result += ","; } } return result; } /** * refer to <a href="http://leepoint.net/notes-java/io/10file/sys-indep-newline.html">System Independent Newline * Characters</a> */ public static String newLineToHtmlBr(String s) { s = StringUtils.replace(s, "\r\n", "<br/>"); return StringUtils.replace(s, "\n", "<br/>"); } }