[align=center][size=large]StringUtils[/size][/align]
package com.java.study.test.unit7;
/**
* String常用工具类
*/
public class StringUtils {
/**
* 为空
* @param str
* @return
*/
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
/**
* 非空
* @param str
* @return
*/
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
/**
* 为空,判断空字符
* @param str
* @return
*/
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 String strip(String str) {
return strip(str, null);
}
public static String stripToNull(String str) {
if (str == null) {
return null;
} else {
str = strip(str, null);
return str.length() != 0 ? str : null;
}
}
public static String stripToEmpty(String str) {
return str != null ? strip(str, null) : "";
}
public static String strip(String str, String stripChars) {
if (isEmpty(str)) {
return str;
} else {
str = stripStart(str, stripChars);
return stripEnd(str, stripChars);
}
}
public static String stripStart(String str, String stripChars) {
int strLen;
if (str == null || (strLen = str.length()) == 0)
return str;
int start = 0;
if (stripChars == null) {
for (; start != strLen && Character.isWhitespace(str.charAt(start)); start++)
;
} else {
if (stripChars.length() == 0)
return str;
for (; start != strLen
&& stripChars.indexOf(str.charAt(start)) != -1; start++)
;
}
return str.substring(start);
}
public static String stripEnd(String str, String stripChars) {
int end;
if (str == null || (end = str.length()) == 0)
return str;
if (stripChars == null) {
for (; end != 0 && Character.isWhitespace(str.charAt(end - 1)); end--)
;
} else {
if (stripChars.length() == 0)
return str;
for (; end != 0 && stripChars.indexOf(str.charAt(end - 1)) != -1; end--)
;
}
return str.substring(0, end);
}
public static String[] stripAll(String strs[]) {
return stripAll(strs, null);
}
public static String[] stripAll(String strs[], String stripChars) {
int strsLen;
if (strs == null || (strsLen = strs.length) == 0)
return strs;
String newArr[] = new String[strsLen];
for (int i = 0; i < strsLen; i++)
newArr[i] = strip(strs[i], stripChars);
return newArr;
}
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 int indexOf(String str, char searchChar) {
if (isEmpty(str))
return -1;
else
return str.indexOf(searchChar);
}
public static int indexOf(String str, char searchChar, int startPos) {
if (isEmpty(str))
return -1;
else
return str.indexOf(searchChar, startPos);
}
public static int indexOf(String str, String searchStr) {
if (str == null || searchStr == null)
return -1;
else
return str.indexOf(searchStr);
}
}