StringUtil
package cn.jorcen.commons.lang;
import java.sql.Date;
import java.sql.Timestamp;
import cn.jorcen.commons.lang1.ClassTypeUtil;
/**
* @author jorcen
* @e-mail mjorcen@gmail.com
* @date 2013-10-28 10:38:23
*/
public class StringUtil {
public StringUtil() {
}
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
public static boolean hasLength(CharSequence str) {
return (str != null && str.length() > 0);
}
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0)
return true;
for (int i = 0; i < strLen; i++)
if (!Character.isWhitespace(str.charAt(i)))
return false;
return true;
}
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
public static String trim(String str) {
return str != null ? str.trim() : null;
}
public static String trimToNull(String str) {
String ts = trim(str);
return isEmpty(ts) ? null : ts;
}
public static String trimToEmpty(String str) {
return str != null ? str.trim() : "";
}
public static boolean equals(String str1, String str2) {
return str1 != null ? str1.equals(str2) : str2 == null;
}
public static boolean equalsIgnoreCase(String str1, String str2) {
return str1 != null ? str1.equalsIgnoreCase(str2) : str2 == null;
}
public static boolean hasText(String str) {
return hasText((CharSequence) str);
}
public static boolean hasText(CharSequence str) {
if (!hasLength(str)) {
return false;
}
int strLen = str.length();
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return true;
}
}
return false;
}
public static String replace(String text, String searchString,
String replacement) {
return replace(text, searchString, replacement, -1);
}
public static String replace(String text, String searchString,
String replacement, int max) {
if (isEmpty(text) || isEmpty(searchString) || replacement == null
|| max == 0)
return text;
int start = 0;
int end = text.indexOf(searchString, start);
if (end == -1)
return text;
int replLength = searchString.length();
int increase = replacement.length() - replLength;
increase = increase >= 0 ? increase : 0;
increase *= max >= 0 ? max <= 64 ? max : 64 : 16;
StringBuffer buf = new StringBuffer(text.length() + increase);
for (; end != -1; end = text.indexOf(searchString, start)) {
buf.append(text.substring(start, end)).append(replacement);
start = end + replLength;
if (--max == 0)
break;
}
buf.append(text.substring(start));
return buf.toString();
}
public static String remove(String str, String remove) {
if (isEmpty(str) || isEmpty(remove))
return str;
else
return replace(str, remove, "", -1);
}
public static String abbr(String str, int maxWidth) {
return abbreviate(str, maxWidth);
}
public static String abbreviate(String str, int maxWidth) {
return abbreviate(str, 0, maxWidth);
}
public static String getStringValue(Object obj, String formate,
String defaultValue) throws Exception {
if (obj == null)
return defaultValue;
switch (ClassTypeUtil.getTypeByClass(obj.getClass())) {
case -1:
throw new Exception((new StringBuilder(String.valueOf(obj
.getClass().getName()))).append(" is undefind ")
.toString());
case 7: // ' 07'
return DateUtil.format((Timestamp) obj, formate);
case 6: // ' 06'
return DateUtil.format((Date) obj, formate);
case 10: // '
'
return DateUtil.format((java.util.Date) obj, formate);
}
return obj.toString();
}
private static String abbreviate(String str, int offset, int maxWidth) {
if (str == null)
return null;
if (maxWidth < 4)
throw new IllegalArgumentException(
"maxWidth must be greater than four");
if (str.length() <= maxWidth)
return str;
if (offset > str.length())
offset = str.length();
if (str.length() - offset < maxWidth - 3)
offset = str.length() - (maxWidth - 3);
if (offset <= 4)
return (new StringBuilder(String.valueOf(str.substring(0,
maxWidth - 3)))).append("...").toString();
if (maxWidth < 7)
throw new IllegalArgumentException(
"Minimum abbreviation width with offset is 7");
if (offset + (maxWidth - 3) < str.length())
return (new StringBuilder("...")).append(
abbreviate(str.substring(offset), maxWidth - 3)).toString();
else
return (new StringBuilder("...")).append(
str.substring(str.length() - (maxWidth - 3))).toString();
}
}