StringUtils
/**
* String tool class
*
* @author ruoyi
*/
public class StringUtils extends org.apache.commons.lang3.StringUtils {<!-- -->
/**
* empty string
*/
private static final String NULLSTR = "";
/**
* underscore
*/
private static final char SEPARATOR = '_';
/**
* get parameter is not empty
*
* @param value defaultValue The value to be judged
* @return value return value
*/
public static <T> T nvl(T value, T defaultValue) {<!-- -->
return value != null ? value : defaultValue;
}
/**
* * Determine whether a Collection is empty, including List, Set, Queue
*
* @param coll Collection to be judged
* @return true: empty false: not empty
*/
public static boolean isEmpty(Collection<?> coll) {<!-- -->
return isNull(coll) || coll.isEmpty();
}
/**
* * Determine whether a Collection is non-empty, including List, Set, Queue
*
* @param coll Collection to be judged
* @return true: not empty false: empty
*/
public static boolean isNotEmpty(Collection<?> coll) {<!-- -->
return !isEmpty(coll);
}
/**
* * Determine whether an object array is empty
*
* @param objects Array of objects to be judged
* * @return true: empty false: not empty
*/
public static boolean isEmpty(Object[] objects) {<!-- -->
return isNull(objects) || (objects. length == 0);
}
/**
* * Determine whether an object array is non-empty
*
* @param objects Array of objects to be judged
* @return true: not empty false: empty
*/
public static boolean isNotEmpty(Object[] objects) {<!-- -->
return !isEmpty(objects);
}
/**
* * Determine whether a Map is empty
*
* @param map Map to be judged
* @return true: empty false: not empty
*/
public static boolean isEmpty(Map<?, ?> map) {<!-- -->
return isNull(map) || map.isEmpty();
}
/**
* * Determine whether a Map is empty
*
* @param map Map to be judged
* @return true: not empty false: empty
*/
public static boolean isNotEmpty(Map<?, ?> map) {<!-- -->
return !isEmpty(map);
}
/**
* * Determine whether a string is an empty string
*
* @param str String
* @return true: empty false: not empty
*/
public static boolean isEmpty(String str) {<!-- -->
return isNull(str) || NULLSTR.equals(str.trim());
}
/**
* * Determine whether a string is a non-empty string
*
* @param str String
* @return true: non-empty string false: empty string
*/
public static boolean isNotEmpty(String str) {<!-- -->
return !isEmpty(str);
}
/**
* * Check if an object is empty
*
* @param object Object
* @return true: empty false: not empty
*/
public static boolean isNull(Object object) {<!-- -->
return object == null;
}
/**
* * Check if an object is non-null
*
* @param object Object
* @return true: not empty false: empty
*/
public static boolean isNotNull(Object object) {<!-- -->
return !isNull(object);
}
/**
* * Determine whether an object is an array type (Java basic type array)
*
* @param object object
* @return true: is an array false: not an array
*/
public static boolean isArray(Object object) {<!-- -->
return isNotNull(object) & amp; & amp; object.getClass().isArray();
}
/**
* remove spaces
*/
public static String trim(String str) {<!-- -->
return (str == null ? "" : str. trim());
}
/**
* Intercept string
*
* @param str string
* @param start start
* @return result
*/
public static String substring(final String str, int start) {<!-- -->
if (str == null) {<!-- -->
return NULLSTR;
}
if (start < 0) {<!-- -->
start = str. length() + start;
}
if (start < 0) {<!-- -->
start = 0;
}
if (start > str. length()) {<!-- -->
return NULLSTR;
}
return str. substring(start);
}
/**
* Intercept string
*
* @param str string
* @param start start
* @param end end
* @return result
*/
public static String substring(final String str, int start, int end) {<!-- -->
if (str == null) {<!-- -->
return NULLSTR;
}
if (end < 0) {<!-- -->
end = str. length() + end;
}
if (start < 0) {<!-- -->
start = str. length() + start;
}
if (end > str. length()) {<!-- -->
end = str. length();
}
if (start > end) {<!-- -->
return NULLSTR;
}
if (start < 0) {<!-- -->
start = 0;
}
if (end < 0) {<!-- -->
end = 0;
}
return str.substring(start, end);
}
/**
* Formatted text, {} represents placeholder<br>
* This method simply replaces the placeholders {} with parameters in order<br>
* If you want to output {}, use \escape {. If you want to output the \ before {}, use double escape character \.<br>
* example:<br>
* Usually use: format("this is {} for {}", "a", "b") -> this is a for b<br>
* Escape {}: format("this is \{} for {}", "a", "b") -> this is \{} for a<br>
* Escape\: format("this is \{} for {}", "a", "b") -> this is \a for b<br>
*
* @param template text template, the replaced part is represented by {}
* @param params parameter value
* @return formatted text
*/
public static String format(String template, Object... params) {<!-- -->
if (isEmpty(params) || isEmpty(template)) {<!-- -->
return template;
}
return StrFormatter.format(template, params);
}
/**
* Underscore to camel case naming
*/
public static String toUnderScoreCase(String str) {<!-- -->
if (str == null) {<!-- -->
return null;
}
StringBuilder sb = new StringBuilder();
// Whether the leading character is capitalized
boolean preCharIsUpperCase = true;
// Whether the current character is uppercase
boolean curreCharIsUpperCase = true;
// Whether the next character is capitalized
boolean nexteCharIsUpperCase = true;
for (int i = 0; i < str. length(); i ++ ) {<!-- -->
char c = str.charAt(i);
if (i > 0) {<!-- -->
preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
} else {<!-- -->
preCharIsUpperCase = false;
}
curreCharIsUpperCase = Character. isUpperCase(c);
if (i < (str. length() - 1)) {<!-- -->
nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
}
if (preCharIsUpperCase & amp; & amp; curreCharIsUpperCase & amp; & amp; !nexteCharIsUpperCase) {<!-- -->
sb.append(SEPARATOR);
} else if ((i != 0 & amp; & amp; !preCharIsUpperCase) & amp; & amp; curreCharIsUpperCase) {<!-- -->
sb.append(SEPARATOR);
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
}
/**
* Whether to contain a string
*
* @param str validation string
* @param strs string group
* @return contains return true
*/
public static boolean inStringIgnoreCase(String str, String... strs) {<!-- -->
if (str != null & amp; & amp; strs != null) {<!-- -->
for (String s : strs) {<!-- -->
if (str. equalsIgnoreCase(trim(s))) {<!-- -->
return true;
}
}
}
return false;
}
/**
* Convert underscore-capitalized strings to camel case. Returns an empty string if the underscore-capitalized string before conversion is empty. For example: HELLO_WORLD->HelloWorld
*
* @param name The string named underline and uppercase before conversion
* @return converted camel case named string
*/
public static String convertToCamelCase(String name) {<!-- -->
StringBuilder result = new StringBuilder();
// quick check
if (name == null || name.isEmpty()) {<!-- -->
// no conversion necessary
return "";
} else if (!name. contains("_")) {<!-- -->
// No underscore, only capitalize the first letter
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
// Split the original string with underscores
String[] camels = name. split("_");
for (String camel : camels) {<!-- -->
// Skip the leading and trailing underscores or double underscores in the original string
if (camel.isEmpty()) {<!-- -->
continue;
}
// capitalize the first letter
result.append(camel.substring(0, 1).toUpperCase());
result.append(camel.substring(1).toLowerCase());
}
return result.toString();
}
/**
* Camel case nomenclature For example: user_name->userName
*/
public static String toCamelCase(String s) {<!-- -->
if (s == null) {<!-- -->
return null;
}
s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
boolean upperCase = false;
for (int i = 0; i < s.length(); i + + ) {<!-- -->
char c = s.charAt(i);
if (c == SEPARATOR) {<!-- -->
upperCase = true;
} else if (upperCase) {<!-- -->
sb.append(Character.toUpperCase(c));
upperCase = false;
} else {<!-- -->
sb.append(c);
}
}
return sb.toString();
}
String formatting
public class StrFormatter {<!-- -->
public static final String EMPTY_JSON = "{}";
public static final char C_BACKSLASH = '';
public static final char C_DELIM_START = '{';
public static final char C_DELIM_END = '}';
/**
* format string<br>
* This method simply replaces the placeholders {} with parameters in order<br>
* If you want to output {}, use \escape {. If you want to output the \ before {}, use double escape character \.<br>
* example:<br>
* Usually used: format("this is {} for {}", "a", "b") -> this is a for b<br>
* Escape {}: format("this is \{} for {}", "a", "b") -> this is \{} for a<br>
* Escape\: format("this is \{} for {}", "a", "b") -> this is \a for b<br>
*
* @param strPattern string template
* @param argArray parameter list
* @return result
*/
public static String format(final String strPattern, final Object... argArray) {<!-- -->
if (StringUtils.isEmpty(strPattern) || StringUtils.isEmpty(argArray)) {<!-- -->
return strPattern;
}
final int strPatternLength = strPattern.length();
//Initialize the defined length for better performance
StringBuilder sbuf = new StringBuilder(strPatternLength + 50);
int handledPosition = 0;
int delimIndex; // location of placeholder
for (int argIndex = 0; argIndex < argArray.length; argIndex + + ) {<!-- -->
delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition);
if (delimIndex == -1) {<!-- -->
if (handledPosition == 0) {<!-- -->
return strPattern;
} else {<!-- --> // The remaining part of the string template no longer contains placeholders, and the result is returned after adding the remaining part
sbuf.append(strPattern, handledPosition, strPatternLength);
return sbuf.toString();
}
} else {<!-- -->
if (delimIndex > 0 & amp; & amp; strPattern.charAt(delimIndex - 1) == C_BACKSLASH) {<!-- -->
if (delimIndex > 1 & amp; & amp; strPattern.charAt(delimIndex - 2) == C_BACKSLASH) {<!-- -->
// There is an escape character before the escape character, and the placeholder is still valid
sbuf.append(strPattern, handledPosition, delimIndex - 1);
sbuf.append(Convert.utf8Str(argArray[argIndex]));
handledPosition = delimIndex + 2;
} else {<!-- -->
// placeholders are escaped
argIndex--;
sbuf.append(strPattern, handledPosition, delimIndex - 1);
sbuf.append(C_DELIM_START);
handledPosition = delimIndex + 1;
}
} else {<!-- -->
// normal placeholder
sbuf.append(strPattern, handledPosition, delimIndex);
sbuf.append(Convert.utf8Str(argArray[argIndex]));
handledPosition = delimIndex + 2;
}
}
}
//Add all characters after the last placeholder
sbuf.append(strPattern, handledPosition, strPattern.length());
return sbuf.toString();
}
}