cn.keayuan.util.StringUtils Maven / Gradle / Ivy
The newest version!
package cn.keayuan.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Iterator;
/**
* String Utils
*/
public class StringUtils {
public static final String NULL = "null";
private StringUtils() {
throw new AssertionError();
}
/**
* is null or its length is 0 or it is made by space
*
* isBlank(null) = true;
* isBlank("") = true;
* isBlank(" ") = true;
* isBlank("a") = false;
* isBlank("a ") = false;
* isBlank(" a") = false;
* isBlank("a b") = false;
*
*
* @param str
* @return if string is null or its size is 0 or it is made by space, return true, else return false.
*/
public static boolean isBlank(String str) {
return (str == null || str.trim().length() == 0);
}
/**
* is null or its length is 0
*
* isEmpty(null) = true;
* isEmpty("") = true;
* isEmpty(" ") = false;
*
*
* @param str CharSequence
* @return if string is null or its size is 0, return true, else return false.
*/
public static boolean isEmpty(CharSequence str) {
return str == null || str.length() == 0;
}
public static boolean notEmpty(CharSequence str) {
return str != null && str.length() > 0;
}
public static String emptyIfNull(String str, String def) {
return str == null ? emptyIfNull(def) : str;
}
public static String emptyIfNull(String str) {
return str == null ? "" : str;
}
public static String join(CharSequence delimiter, Object... obj) {
StringBuilder builder = new StringBuilder();
for (Object s : obj) {
builder.append(s).append(delimiter);
}
if (builder.length() > 0) {
builder.setLength(builder.length() - delimiter.length());
}
return builder.toString();
}
public static String join(CharSequence delimiter, Iterable> tokens) {
final Iterator> it = tokens.iterator();
if (!it.hasNext()) {
return "";
}
final StringBuilder sb = new StringBuilder();
sb.append(it.next());
while (it.hasNext()) {
sb.append(delimiter);
sb.append(it.next());
}
return sb.toString();
}
/**
* compare two string
*
* @see ObjectUtils#equals(Object, Object)
*/
public static boolean equals(String actual, String expected) {
return ObjectUtils.equals(actual, expected);
}
/**
* get length of CharSequence
*
* length(null) = 0;
* length(\"\") = 0;
* length(\"abc\") = 3;
*
*
* @return if str is null or empty, return 0, else return {@link CharSequence#length()}.
*/
public static int length(CharSequence str) {
return str == null ? 0 : str.length();
}
/**
* capitalize first letter
*
* capitalizeFirstLetter(null) = null;
* capitalizeFirstLetter("") = "";
* capitalizeFirstLetter("2ab") = "2ab"
* capitalizeFirstLetter("a") = "A"
* capitalizeFirstLetter("ab") = "Ab"
* capitalizeFirstLetter("Abc") = "Abc"
*
*/
public static String capitalizeFirstLetter(String str) {
if (isEmpty(str)) {
return str;
}
char c = str.charAt(0);
return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str : Character.toUpperCase(c) + str.substring(1);
}
/**
* encoded in utf-8
*
* utf8Encode(null) = null
* utf8Encode("") = "";
* utf8Encode("aa") = "aa";
* utf8Encode("啊啊啊啊") = "%E5%95%8A%E5%95%8A%E5%95%8A%E5%95%8A";
*
*/
public static String utf8Encode(String str) {
if (!isEmpty(str) && str.getBytes().length != str.length()) {
try {
return URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UnsupportedEncodingException occurred. ", e);
}
}
return str;
}
/**
* encoded in utf-8, if exception, return defaultReturn
*/
public static String utf8Encode(String str, String def) {
if (!isEmpty(str) && str.getBytes().length != str.length()) {
try {
return URLEncoder.encode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
return def;
}
}
return str;
}
/**
* process special char in html
*
* htmlEscapeCharsToString(null) = null;
* htmlEscapeCharsToString("") = "";
* htmlEscapeCharsToString("mp3") = "mp3";
* htmlEscapeCharsToString("mp3<") = "mp3<";
* htmlEscapeCharsToString("mp3>") = "mp3>";
* htmlEscapeCharsToString("mp3&mp4") = "mp3&mp4";
* htmlEscapeCharsToString("mp3"mp4") = "mp3\"mp4";
* htmlEscapeCharsToString("mp3<>&"mp4") = "mp3<>&\"mp4";
*
*
* @param source -
* @return -
*/
public static String htmlEscapeCharsToString(String source) {
return isEmpty(source) ? source : source.replace("<", "<")
.replace(">", ">")
.replace("&", "&")
.replace(""", "\"");
}
public static String getStackTraceString(Throwable tr) {
if (tr == null) {
return NULL;
}
Throwable t = tr;
while (t != null) {
if (t instanceof UnknownHostException) {
return t.toString();
}
t = t.getCause();
}
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
tr.printStackTrace(pw);
pw.flush();
return sw.toString();
}
/**
* 任意类型转字符串
*
* @param obj 数据
*/
public static String valueOf(Object obj) {
StringBuilder builder = new StringBuilder();
objToString(builder, obj);
return builder.toString();
}
private static void objToString(StringBuilder builder, Object obj) {
if (builder == null) {
return;
}
if (obj == null) {
builder.append(NULL);
} else if (obj instanceof Throwable) {
builder.append('\n');
builder.append(getStackTraceString((Throwable) obj));
} else if (obj.getClass().isArray()) {
builder.append('[');
if (Array.getLength(obj) > 0) {
arrayString(builder, obj);
builder.setLength(builder.length() - 1);
}
builder.append(']');
} else if (obj instanceof String) {
builder.append((String) obj);
} else {
builder.append(obj);
}
}
private static void arrayString(StringBuilder builder, Object obj) {
if (obj instanceof boolean[]) {
boolString(builder, (boolean[]) obj);
Arrays.toString((boolean[]) obj);
} else if (obj instanceof byte[]) {
byteString(builder, (byte[]) obj);
} else if (obj instanceof char[]) {
charString(builder, (char[]) obj);
} else if (obj instanceof short[]) {
shortString(builder, (short[]) obj);
} else if (obj instanceof int[]) {
intString(builder, (int[]) obj);
} else if (obj instanceof float[]) {
floatString(builder, (float[]) obj);
} else if (obj instanceof long[]) {
longString(builder, (long[]) obj);
} else if (obj instanceof double[]) {
doubleString(builder, (double[]) obj);
} else {
objectString(builder, (Object[]) obj);
}
}
private static void boolString(StringBuilder builder, boolean[] value) {
for (boolean b : value) {
builder.append(b).append(',');
}
}
private static void byteString(StringBuilder builder, byte[] value) {
for (byte b : value) {
builder.append(Integer.toHexString(b & 0xff)).append(',');
}
}
private static void charString(StringBuilder builder, char[] value) {
for (char b : value) {
builder.append(b).append(',');
}
}
private static void shortString(StringBuilder builder, short[] value) {
for (short b : value) {
builder.append(b).append(',');
}
}
private static void intString(StringBuilder builder, int[] value) {
for (int b : value) {
builder.append(b).append(',');
}
}
private static void floatString(StringBuilder builder, float[] value) {
for (float b : value) {
builder.append(b).append(',');
}
}
private static void longString(StringBuilder builder, long[] value) {
for (long b : value) {
builder.append(b).append(',');
}
}
private static void doubleString(StringBuilder builder, double[] value) {
for (double b : value) {
builder.append(b).append(',');
}
}
private static void objectString(StringBuilder builder, Object[] value) {
for (Object b : value) {
objToString(builder, b);
builder.append(',');
}
}
}