import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
* 字符串处理工具类
* @author
*
*/
public class StringUtils {
/** 空字符串。 */
public static final String EMPTY_STRING = "";
private static final char[] LETTER_TABLE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
private static final char[] NUMBER_TABLE = "0123456789".toCharArray();
private StringUtil() {
throw new IllegalStateException("Utility class");
}
/*
* =========================================================================
* ===
*/
/* 判空函数。 */
/* */
/* 以下方法用来判定一个字符串是否为: */
/* 1. null */
/* 2. empty - "" */
/* 3. blank - "全部是空白" - 空白由Character.isWhitespace所定义。 */
/*
* =========================================================================
* ===
*/
/**
*
* StringUtil.isEmpty(null) = true StringUtil.isEmpty("") = true
* StringUtil.isEmpty(" ") = false StringUtil.isEmpty("bob") = false
* StringUtil.isEmpty(" bob ") = false
*
* @param str
* 要检查的字符串
*
* @return 如果为空, 则返回<code>true</code>
*/
public static boolean isEmpty(String str) {
return ((str == null) || (str.length() == 0));
}
/**
* StringUtil.isEmpty(null) = false StringUtil.isEmpty("") = false
* StringUtil.isEmpty(" ") = true StringUtil.isEmpty("bob") = true
* StringUtil.isEmpty(" bob ") = true
*
* @param str
* 要检查的字符串
*
* @return 如果不为空, 则返回<code>true</code>
*/
public static boolean isNotEmpty(String str) {
return ((str != null) && (str.length() > 0));
}
/**
* StringUtil.isBlank(null) = true StringUtil.isBlank("") = true
* StringUtil.isBlank(" ") = true StringUtil.isBlank("bob") = false
* StringUtil.isBlank(" bob ") = false
*
* @return 如果为空白, 则返回<code>true</code>
*/
public static boolean isBlank(String str) {
int length;
if ((str == null) || ((length = str.length()) == 0)) {
return true;
}
for (int i = 0; i < length; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* StringUtil.isBlank(null) = false StringUtil.isBlank("") = false
* StringUtil.isBlank(" ") = false StringUtil.isBlank("bob") = true
* StringUtil.isBlank(" bob ") = true
*
* @param str
* 要检查的字符串
* @return 如果为空白, 则返回<code>true</code>
*/
public static boolean isNotBlank(String str) {
int length;
if ((str == null) || ((length = str.length()) == 0)) {
return false;
}
for (int i = 0; i < length; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return true;
}
}
return false;
}
public static String nvl(Object o) {
return nvl(o, "");
}
public static String nvl(Object o, String def) {
Object obj = o == null ? def : o;
return obj instanceof String ? (String) obj : obj.toString();
}
public static String nvl2(Object o, String def) {
String s = nvl(o, def);
return isEmpty(s) ? def : s;
}
/**
* 拼接数组
* @param strs 字符串数组
* @param sep 拼接间隔字符
* @return
*/
public static String join(String[] strs, String sep) {
StringBuilder sb = new StringBuilder();
if (strs != null && strs.length > 0) {
sb.append(strs[0]);
if (strs.length > 1) {
for (int i = 1; i < strs.length; i++) {
sb.append(sep).append(strs[i]);
}
}
}
return sb.toString();
}
/**
*
* @param list
* @param sep
* @return
*/
public static <E> String join(List<E> list, String sep) {
StringBuilder sb = new StringBuilder();
if (list != null && !list.isEmpty()) {
sb.append(list.get(0));
int len = list.size();
if (len > 1) {
for (int i = 1; i < len; i++) {
sb.append(sep).append(list.get(i));
}
}
}
return sb.toString();
}
/**
*
* @param count
* @param s
* @param sep
* @return
*/
public static String join(int count, String s, String sep) {
StringBuilder sb = new StringBuilder();
if (count > 0) {
sb.append(s);
if (count > 1) {
for (int i = 1; i < count; i++) {
sb.append(sep).append(s);
}
}
}
return sb.toString();
}
/**
*
* @param strs
* @param pre
* @param sep
* @return
*/
public static String joinBefore(String[] strs, String pre, String sep) {
StringBuilder sb = new StringBuilder();
if (strs != null && strs.length > 0) {
sb.append(pre).append(strs[0]);
if (strs.length > 1) {
for (int i = 1; i < strs.length; i++) {
sb.append(sep).append(pre).append(strs[i]);
}
}
}
return sb.toString();
}
/**
*
* @param strs
* @param suf
* @param sep
* @return
*/
public static String joinAfter(String[] strs, String suf, String sep) {
StringBuilder sb = new StringBuilder();
if (strs != null && strs.length > 0) {
sb.append(strs[0]).append(suf);
if (strs.length > 1) {
for (int i = 1; i < strs.length; i++) {
sb.append(sep).append(strs[i]).append(suf);
}
}
}
return sb.toString();
}
/**
*
* @param strs1
* @param con
* @param strs2
* @param sep
* @return
*/
public static String join(String[] strs1, String con, String[] strs2, String sep) {
StringBuilder sb = new StringBuilder();
if (strs1 != null && strs1.length > 0) {
sb.append(strs1[0]).append(con).append(strs2[0]);
if (strs1.length > 1) {
for (int i = 1; i < strs1.length; i++) {
sb.append(sep).append(strs1[i]).append(con).append(strs2[i]);
}
}
}
return sb.toString();
}
/**
*
* @param s
* @param ch
* @param length
* @return
*/
public static String lpad(String s, char ch, int length) {
return pad(s, ch, length, true);
}
/**
*
* @param s
* @param ch
* @param length
* @return
*/
public static String rpad(String s, char ch, int length) {
return pad(s, ch, length, false);
}
/**
*
* @param s
* @param ch
* @param length
* @param left
* @return
*/
private static String pad(String s, char ch, int length, boolean left) {
String str = nvl(s);
if(s==null) {
return str;
}
if (s.length() < length) {
char[] buf = new char[length - s.length()];
Arrays.fill(buf, ch);
str = left ? new String(buf) + str : str + new String(buf);
}
return str;
}
/**
*
* @param length
* @return
*/
public static String getRandomLetter(int length) {
return getRandomStr(LETTER_TABLE, length);
}
/**
*
* @param length
* @return
*/
public static String getRandomNumber(int length) {
return getRandomStr(NUMBER_TABLE, length);
}
/**
*
* @param set
* @param length
* @return
*/
public static String getRandomStr(char[] set, int length) {
String result = "";
if (set != null && set.length > 0 && length > 0) {
char[] ch = new char[length];
int len = set.length;
Random r = new Random();
for (int i = 0; i < length; i++) {
ch[i] = set[r.nextInt(len)];
}
result = new String(ch);
}
return result;
}
/**
*
* @param s
* @param fromCharset
* @param toCharset
* @return
*/
public static String convertCharset(String s, String fromCharset, String toCharset) {
String result = null;
if (s != null) {
try {
result = new String(s.getBytes(fromCharset), toCharset);
} catch (UnsupportedEncodingException e) {
result = s;
}
}
return result;
}
/**
*
* @param str
* @param from
* @param to
* @return
*/
public static String replace(String str, String from, String to) {
return str == null ? null : str.replace(from, to);
}
/**
*
* @param obj
* @return
*/
public static String getObjectDesc4Log(Object obj) {
String s = null;
if (obj == null) {
s = "<null>";
} else {
if (obj instanceof String) {
s = (String) obj;
} else if (obj instanceof byte[]) {
s = "<binary data, length=" + ((byte[]) obj).length + ">";
} else {
s = obj.toString();
}
if (s.length() > 255) {
s = s.substring(0, 150) + " ...... " + s.substring(s.length() - 100);
}
}
return s;
}
/**
* 把list转换为一个用逗号分隔的字符串
* @param list
* @return
*/
@SuppressWarnings("rawtypes")
public static String listToString(List list) {
StringBuilder sb = new StringBuilder();
if (list != null && !list.isEmpty()) {
for (int i = 0; i < list.size(); i++) {
if (i < list.size() - 1) {
sb.append(list.get(i) + ",");
} else {
sb.append(list.get(i));
}
}
}
return sb.toString();
}
/**
* unicode 转字符串
* @param unicode
* @return
*/
public static String unicodeNative(String unicode) {
String[] asciis = unicode.split("\\\\u");
StringBuilder nativeValue = new StringBuilder();
nativeValue.append(asciis[0]);
try {
for (int i = 1; i < asciis.length; i++) {
String code = asciis[i];
nativeValue.append((char) Integer.parseInt(code.substring(0, 4), 16));
if (code.length() > 4) {
nativeValue.append(code.substring(4, code.length()));
}
}
} catch (NumberFormatException e) {
return unicode;
}
return nativeValue.toString();
}
}
字符串处理工具类
最新推荐文章于 2024-08-15 16:46:51 发布