/***
* 模仿log.info的方法,将参数传入括号内
* @param content
* @param params
* @return
*/
public static String logInfo(String content, Object... params) {
if (content == null) throw new NullPointerException("msg");
StringBuffer sb = new StringBuffer();
final String delimiter = "{}";
int cnt = 0;//括号出现的计数值
if (params != null) {
for (int i = 0; i < params.length + 1; i++) {
int tmpIndex = content.indexOf(delimiter);
if (tmpIndex == -1) {
//不存在赋值
sb.append(content);
break;
} else {
//存在则进行赋值拼接
String str = content.substring(0, tmpIndex);
content = content.substring((tmpIndex + 2), content.length());
String valStr = params[i].toString();
sb.append(str).append(valStr);
cnt++;
}
}
} else {
//param为空
sb.append(content);
}
return sb.toString();
}
测试:
public static void main(String[] args) {
System.out.println(logInfo("测试:{},info:{}", "哈哈", "看看"));
}
输出: