1. 程式人生 > >模仿log4j的日誌字串格式化log(format,args)

模仿log4j的日誌字串格式化log(format,args)

什麼也不說,直接看程式碼。

import java.util.Collection;
import net.xuele.common.utils.format.JsonUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.ObjectUtils;

/**
 * 字串格式化
 * @author wull
 */
public class StringFormat {

	/**
	 * 模仿C#格式化字串
	 *
	 * @param str
	 * @param args
	 * @return
	 */
	public static String format(String str, String... args) {
		for (int i = 0; i < args.length; i++) {
			str = str.replaceFirst("\\{\\}", args[i]);
		}
		return str;
	}

	public static String format(String str, Object... args) {
		for (int i = 0; i < args.length; i++) {
			str = str.replaceFirst("\\{\\}", String.valueOf(args[i]));
		}
		return str;
	}

	/**
	 * 安全連結字串
	 *
	 * @param strs
	 * @return
	 */
	public static String softLink(String... strs) {
		StringBuffer sb = new StringBuffer();
		for (String s : strs) {
			sb.append(StringUtils.isEmpty(s) ? "" : s);
		}
		return sb.toString();
	}

	/**
	 * 安全去首位連結字串
	 *
	 * @param strs
	 * @return
	 */
	public static String softTrimLink(String... strs) {
		StringBuffer sb = new StringBuffer();
		for (String s : strs) {
			sb.append(StringUtils.trimToEmpty(s));
		}
		return sb.toString();
	}

	/**
	 * 安全toString
	 *
	 * @param str
	 * @return
	 */
	public static String toString(Object str) {
		if (null == str)
			return null;
		return str.toString();
	}

	/**
	 * 通過","連結List
	 *
	 * @param strlist
	 * @return
	 */
	public static String formatByComma(Collection<String> strlist) {
		if (CollectionUtils.isEmpty(strlist))
			return "";
		String[] strs = new String[strlist.size()];
		strlist.toArray(strs);
		return StringFormat.formatByComma(strs);
	}

	/**
	 * 將陣列輕而易舉的轉換成用逗號分隔的字串
	 *
	 * @param strs
	 * @return
	 */
	public static String formatByComma(String... strs) {
		return StringFormat.formatByComma(true, strs);
	}

	/**
	 * 將陣列輕而易舉的轉換成用逗號分隔的字串
	 *
	 * @param strs
	 * @return
	 */
	public static String formatByComma(boolean filterNull, String... strs) {
		if (!filterNull)
			ArrayUtils.removeElement(strs, null);
		String str = ArrayUtils.toString(strs);
		return StringUtils.substring(str, 1, str.length() - 1);
	}

	/**
	 * 在陣列每個元素前後追加資料
	 *
	 * @param array
	 * @param head
	 * @param end
	 * @return
	 */
	public static String[] formatEcho(String head, String end, String... array) {
		if (ObjectUtils.isEmpty(array))
			return null;
		for (int i = 0; i < array.length; i++) {
			if (null == array[i])
				continue;
			array[i] = head + array[i] + end;
		}
		return array;
	}

	/**
	 * 把用","分割的字串,轉換成陣列
	 *
	 * @param str
	 * @return
	 */
	public static String[] splitByComma(String str) {
		return StringUtils.split(str, ",");
	}

	public static void main(String[] args) {
		String[] stringArray = { "Red", "Orange", "Blue", "Brown", "Red" };
		System.out.println(StringFormat.formatByComma(stringArray));

		String unitId="unitId";
		Integer sort=0;
		String parentIds="parentIds";
		String str = StringFormat.format("查詢原題對應的衍生題param,,unitId={},sort={},parentIds={}", new Object[]{unitId, sort, JsonUtils.toJson(parentIds)});
		System.out.println(str);
	}
}