![JAR search and dependency download from the Maven repository](/logo.png)
com.yuxuan66.core.utils.str.StrUtil Maven / Gradle / Ivy
package com.yuxuan66.core.utils.str;
/**
*
* @ClassName: StrUtil
* @Description:(字符串工具类)
* @author Sir丶雨轩
* @date 2018年8月9日 下午4:37:43
*
*/
public final class StrUtil {
public static final String EMPTY_JSON = "{}";
private StrUtil() {
}
/**
*
* @Title: format
* @Description: (对占位符进行格式化输出)
* @author Sir丶雨轩
* @param template
* @param params
* @return
*/
public static String format(String strPattern, String... argArray) {
if (strPattern == null || !strPattern.contains(EMPTY_JSON) || argArray == null || argArray.length == 0) {
return strPattern;
}
final int strPatternLength = strPattern.length();
// 初始化定义好的长度以获得更好的性能
StringBuilder sbuf = new StringBuilder(strPatternLength + 50);
int handledPosition = 0;// 记录已经处理到的位置
int delimIndex;// 占位符所在位置
for (int argIndex = 0; argIndex < argArray.length; argIndex++) {
delimIndex = strPattern.indexOf(StrUtil.EMPTY_JSON, handledPosition);
if (delimIndex == -1) {// 剩余部分无占位符
if (handledPosition == 0) { // 不带占位符的模板直接返回
return strPattern;
} else { // 字符串模板剩余部分不再包含占位符,加入剩余部分后返回结果
sbuf.append(strPattern, handledPosition, strPatternLength);
return sbuf.toString();
}
} else {
sbuf.append(strPattern, handledPosition, delimIndex);
sbuf.append(argArray[argIndex]);
handledPosition = delimIndex + 2;
}
}
// append the characters following the last {} pair.
// 加入最后一个占位符后所有的字符
sbuf.append(strPattern, handledPosition, strPattern.length());
return sbuf.toString();
}
/**
*
* @Title: isEmpty
* @Description: (判断字符串是否为空)
* @author Sir丶雨轩
* @param str
* @return
*/
public static boolean isEmpty(String str) {
if (str == null || "".equals(str) || "".equals(str.trim())) {
return true;
}
return false;
}
/**
*
* @Title: isNotEmpty
* @Description: (判断字符串是否不为空)
* @author Sir丶雨轩
* @param str
* @return
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy