org.onetwo.common.utils.StringUtils Maven / Gradle / Ivy
package org.onetwo.common.utils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.onetwo.common.file.FileUtils;
import org.onetwo.common.reflect.ReflectUtils;
import org.onetwo.common.utils.func.ReturnableClosure;
@SuppressWarnings({ "rawtypes", "unchecked" })
public abstract class StringUtils {
public static final String ENCODING = "UTF-8";
public static final String EMPTY = "";
public static boolean isNullOrBlankString(Object value){
return value==null || (String.class.isInstance(value) && org.apache.commons.lang3.StringUtils.isBlank(value.toString()));
}
public static String emptyIfNull(Object str){
return str==null?EMPTY:str.toString();
}
public static String lowerCase(String str) {
if (str == null) {
return EMPTY;
}
return str.toLowerCase();
}
public static String encode(String str) {
if (StringUtils.isBlank(str))
return "";
try {
return URLEncoder.encode(str, ENCODING);
} catch (Exception e) {
return str;
}
}
public static String decode(String str) {
try {
return URLDecoder.decode(str, ENCODING);
} catch (UnsupportedEncodingException e) {
return str;
}
}
public static boolean isObjectBlank(Object obj) {
return obj == null || obj.toString().trim().equals("");
}
public static boolean isBlank(String str) {
return str == null || str.trim().equals("");
}
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
public static boolean hasLength(CharSequence str) {
return (str != null && str.length() > 0);
}
public static boolean hasLength(String str) {
return hasLength((CharSequence) str);
}
public static String bar2UnderLine(String str) {
return str.replace('-', '_');
}
public static String getLastName(String clsName, String sep) {
int index = clsName.lastIndexOf(sep);
if (index != -1)
return clsName.substring(index);
else
return clsName;
}
public static String getFirstWord(String str) {
if(StringUtils.isBlank(str))
return LangUtils.EMPTY_STRING;
char[] chars = str.toCharArray();
StringBuilder first = new StringBuilder();
first.append(chars[0]);
for (int i = 1; i < chars.length; i++) {
if(Character.isUpperCase(chars[i])){
break;
}else{
first.append(chars[i]);
}
}
return first.toString();
}
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 append(String... strings) {
if (strings == null || strings.length == 0)
return "";
StringBuilder sb = new StringBuilder();
for (String str : strings)
sb.append(str);
return sb.toString();
}
public static boolean equals(String str1, String str2) {
return str1 == null ? str2 == null : str1.equals(str2);
}
public static String capitalize(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return str;
}
return new StringBuffer(strLen).append(Character.toTitleCase(str.charAt(0))).append(str.substring(1)).toString();
}
public static String uncapitalize(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return str;
}
return new StringBuffer(strLen).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
}
public static String toPropertyName(String str) {
return toCamel(str, false);
}
public static String toClassName(String str) {
return toCamel(str, true);
}
public static String toCamel(String str, boolean isFirstUpper) {
return toCamel(str, '_', isFirstUpper);
}
public static String toCamel(String str, char op, boolean isFirstUpper) {
if (str.indexOf(op) == -1) {
str = str.toLowerCase();
if (isFirstUpper && Character.isLowerCase(str.charAt(0))) {
return str.substring(0, 1).toUpperCase() + str.substring(1);
} else {
return str;
}
}
char[] chars = str.toCharArray();
StringBuilder newStr = new StringBuilder();
boolean needUpper = isFirstUpper;
for (int i = 0; i < chars.length; i++) {
char c = Character.toLowerCase(chars[i]);
if (needUpper) {
c = Character.toUpperCase(c);
needUpper = false;
}
if (c == op) {
needUpper = true;
continue;
}
newStr.append(c);
}
return newStr.toString();
}
public static boolean hasUpper(String str) {
char[] chars = str.toCharArray();
for (char ch : chars) {
if (Character.isUpperCase(ch))
return true;
}
return false;
}
public static String convert2UnderLineName(String name) {
return convertWithSeperator(name, "_");
}
public static String convertWithSeperator(String name, String op) {
if(name==null)
return "";
StringBuffer table = new StringBuffer();
char[] chars = name.toCharArray();
table.append(Character.toLowerCase(chars[0]));
for (int i = 1; i < chars.length; i++) {
char ch = chars[i];
if (Character.isUpperCase(ch)) {
table.append(op);
ch = Character.toLowerCase(ch);
}
table.append(ch);
}
return table.toString();
}
public static String getSimpleBeanName(String clsName) {
if (clsName.indexOf('.') == -1)
return clsName;
int index = clsName.lastIndexOf('.');
String sn = clsName.substring(index + 1);
return uncapitalize(sn);
}
public static String getClassShortName(Object obj) {
Assert.notNull(obj);
Class> cls = ReflectUtils.getObjectClass(obj);
return uncapitalize(cls.getSimpleName());
}
/*
* from apache common org.apache.commons.lang3.StringUtils
*
*
*/
public static String[] split(String str, char separatorChar) {
return splitWorker(str, separatorChar, false);
}
private static String[] splitWorker(String str, char separatorChar, boolean preserveAllTokens) {
// Performance tuned for 2.0 (JDK1.4)
if (str == null) {
return null;
}
int len = str.length();
if (len == 0) {
return ArrayUtils.EMPTY_STRING_ARRAY;
}
List list = new ArrayList();
int i = 0, start = 0;
boolean match = false;
boolean lastMatch = false;
while (i < len) {
if (str.charAt(i) == separatorChar) {
if (match || preserveAllTokens) {
list.add(str.substring(start, i));
match = false;
lastMatch = true;
}
start = ++i;
continue;
} else {
lastMatch = false;
}
match = true;
i++;
}
if (match || (preserveAllTokens && lastMatch)) {
list.add(str.substring(start, i));
}
return (String[]) list.toArray(new String[list.size()]);
}
public static List splitWithRetainSeparator(String str, String separatorChars, String retainSeparator) {
if (separatorChars == null) {
separatorChars = " ";
}
separatorChars += trimToEmpty(retainSeparator);
int len = str.length();
int wordIndex = 0;
List strlist = new ArrayList();
String w = null;
char ch;
for (int i = 0; i < len; i++) {
ch = str.charAt(i);
if (separatorChars.indexOf(ch) != -1) {
w = str.substring(wordIndex, i);
strlist.add(w);
if (retainSeparator.indexOf(ch) != -1) {
strlist.add(String.valueOf(ch));
}
wordIndex = i + 1;
}
}
if (wordIndex < len) {
w = str.substring(wordIndex);
strlist.add(w);
}
return strlist;
}
public static String[] split(String str, String separatorChars) {
return splitWorker(str, separatorChars, -1, false);
}
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
public static String substring(String str, int beginIndex, int endIndex) {
Assert.isTrue(beginIndex=str.length())
return EMPTY;
if(endIndex>str.length()){
endIndex = str.length();
}
return str.substring(beginIndex, endIndex);
}
public static String substringAfter(String str, String separator) {
if (isEmpty(str)) {
return str;
}
if (separator == null) {
return EMPTY;
}
int pos = str.indexOf(separator);
if (pos == -1) {
return EMPTY;
}
return str.substring(pos + separator.length());
}
public static String substringBefore(String str, String separator) {
if (isEmpty(str) || separator == null) {
return str;
}
if (separator.length() == 0) {
return EMPTY;
}
int pos = str.indexOf(separator);
if (pos == -1) {
return str;
}
return str.substring(0, pos);
}
public static String substringBefore(String str, String separator, int fromIndex) {
if (isEmpty(str) || separator == null) {
return str;
}
if (separator.length() == 0) {
return EMPTY;
}
int pos = str.indexOf(separator, fromIndex);
if (pos == -1) {
return str;
}
return str.substring(0, pos);
}
public static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
// Performance tuned for 2.0 (JDK1.4)
// Direct code is quicker than StringTokenizer.
// Also, StringTokenizer uses isSpace() not isWhitespace()
if (str == null) {
return null;
}
int len = str.length();
if (len == 0) {
return ArrayUtils.EMPTY_STRING_ARRAY;
}
List list = new ArrayList();
int sizePlus1 = 1;
int i = 0, start = 0;
boolean match = false;
boolean lastMatch = false;
if (separatorChars == null) {
// Null separator means use whitespace
while (i < len) {
if (Character.isWhitespace(str.charAt(i))) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
} else {
lastMatch = false;
}
match = true;
i++;
}
} else if (separatorChars.length() == 1) {
// Optimise 1 character case
char sep = separatorChars.charAt(0);
while (i < len) {
if (str.charAt(i) == sep) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
} else {
lastMatch = false;
}
match = true;
i++;
}
} else {
// standard case
while (i < len) {
if (separatorChars.indexOf(str.charAt(i)) >= 0) {
if (match || preserveAllTokens) {
lastMatch = true;
if (sizePlus1++ == max) {
i = len;
lastMatch = false;
}
list.add(str.substring(start, i));
match = false;
}
start = ++i;
continue;
} else {
lastMatch = false;
}
match = true;
i++;
}
}
if (match || (preserveAllTokens && lastMatch)) {
list.add(str.substring(start, i));
}
return (String[]) list.toArray(new String[list.size()]);
}
public static int indexOfAny(String str, String... searchStrs) {
if ((str == null) || (searchStrs == null)) {
return -1;
}
int sz = searchStrs.length;
// String's can't have a MAX_VALUEth index.
int ret = Integer.MAX_VALUE;
int tmp = 0;
for (int i = 0; i < sz; i++) {
String search = searchStrs[i];
if (search == null) {
continue;
}
tmp = str.indexOf(search);
if (tmp == -1) {
continue;
}
if (tmp < ret) {
ret = tmp;
}
}
return (ret == Integer.MAX_VALUE) ? -1 : ret;
}
public static String buildString(Object obj) {
if (obj == null)
return "null";
StringBuilder toString = new StringBuilder("{");
String str = null;
if (Collection.class.isAssignableFrom(obj.getClass()) || obj.getClass().isArray()) {
int index = 0;
List list = asList(obj);
if (list == null)
return "null";
for (Object o : (Collection) list) {
str = buildString(o);
if (StringUtils.isBlank(str))
continue;
if (index != 0)
toString.append(", ");
toString.append(str);
index++;
}
} else if (Map.class.isAssignableFrom(obj.getClass())) {
int index = 0;
for (Object o : ((Map) obj).entrySet()) {
str = buildString(o);
if (StringUtils.isBlank(str))
continue;
if (index != 0)
toString.append(", ");
toString.append(str);
}
} else {
str = obj.toString();
return "[" + str + "]";
}
toString.append("}");
return toString.toString();
}
public static List asList(Object array) {
if (array == null)
return null;
List list = null;
if (Collection.class.isAssignableFrom(array.getClass())) {
if (List.class.isAssignableFrom(array.getClass()))
list = (List) array;
else {
list = new ArrayList();
list.addAll((Collection) array);
}
} else if (array.getClass().isArray()) {
list = new ArrayList();
for (Object o : (Object[]) array) {
list.add(o);
}
} else {
list = new ArrayList();
list.add(array);
}
return (list == null || list.isEmpty()) ? null : list;
}
public static String join(String separator, Object... array) {
if (array == null) {
return EMPTY;
}
return join(array, separator, 0, array.length);
}
public static String join(Object[] array, String separator) {
if (array == null) {
return EMPTY;
}
return join(array, separator, 0, array.length);
}
public static String join(Object[] array, String separator, ReturnableClosure
© 2015 - 2024 Weber Informatics LLC | Privacy Policy