package com.github.lazylibrary.util; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.security.MessageDigest; import java.util.ArrayList; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; /** *
* isBlank(null) = true;
* isBlank("") = true;
* isBlank(" ") = true;
* isBlank("a") = false;
* isBlank("a ") = false;
* isBlank(" a") = false;
* isBlank("a b") = false;
*
*
* @param str 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 str
* @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);
}
/**
* get length of CharSequence
*
*
* length(null) = 0;
* length(\"\") = 0;
* length(\"abc\") = 3;
*
*
* @param str str
* @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();
}
/**
* null Object to empty string
*
*
* nullStrToEmpty(null) = "";
* nullStrToEmpty("") = "";
* nullStrToEmpty("aa") = "aa";
*
*
* @param str str
* @return String
*/
public static String nullStrToEmpty(Object str) {
return (str == null
? ""
: (str instanceof String ? (String) str : str.toString()));
}
/**
* @param str str
* @return String
*/
public static String capitalizeFirstLetter(String str) {
if (isEmpty(str)) {
return str;
}
char c = str.charAt(0);
return (!Character.isLetter(c) || Character.isUpperCase(c))
? str
: new StringBuilder(str.length()).append(
Character.toUpperCase(c))
.append(str.substring(1))
.toString();
}
/**
* encoded in utf-8
*
* @param str 字符串
* @return 返回一个utf8的字符串
*/
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;
}
/**
* @param href 字符串
* @return 返回一个html
*/
public static String getHrefInnerHtml(String href) {
if (isEmpty(href)) {
return "";
}
String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*";
Pattern hrefPattern = Pattern.compile(hrefReg,
Pattern.CASE_INSENSITIVE);
Matcher hrefMatcher = hrefPattern.matcher(href);
if (hrefMatcher.matches()) {
return hrefMatcher.group(1);
}
return href;
}
/**
* @param source 字符串
* @return 返回htmL到字符串
*/
public static String htmlEscapeCharsToString(String source) {
return StringUtils.isEmpty(source)
? source
: source.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll("&", "&")
.replaceAll(""", "\"");
}
/**
* @param s str
* @return String
*/
public static String fullWidthToHalfWidth(String s) {
if (isEmpty(s)) {
return s;
}
char[] source = s.toCharArray();
for (int i = 0; i < source.length; i++) {
if (source[i] == 12288) {
source[i] = ' ';
// } else if (source[i] == 12290) {
// source[i] = '.';
}
else if (source[i] >= 65281 && source[i] <= 65374) {
source[i] = (char) (source[i] - 65248);
}
else {
source[i] = source[i];
}
}
return new String(source);
}
/**
* @param s 字符串
* @return 返回的数值
*/
public static String halfWidthToFullWidth(String s) {
if (isEmpty(s)) {
return s;
}
char[] source = s.toCharArray();
for (int i = 0; i < source.length; i++) {
if (source[i] == ' ') {
source[i] = (char) 12288;
// } else if (source[i] == '.') {
// source[i] = (char)12290;
}
else if (source[i] >= 33 && source[i] <= 126) {
source[i] = (char) (source[i] + 65248);
}
else {
source[i] = source[i];
}
}
return new String(source);
}
/**
* @param str 资源
* @return 特殊字符串切换
*/
public static String replaceBlanktihuan(String str) {
String dest = "";
if (str != null) {
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
/**
* 判断给定的字符串是否为null或者是空的
*
* @param string 给定的字符串
*/
public static boolean isEmpty(String string) {
return string == null || "".equals(string.trim());
}
/**
* 判断给定的字符串是否不为null且不为空
*
* @param string 给定的字符串
*/
public static boolean isNotEmpty(String string) {
return !isEmpty(string);
}
/**
* 判断给定的字符串数组中的所有字符串是否都为null或者是空的
*
* @param strings 给定的字符串
*/
public static boolean isEmpty(String... strings) {
boolean result = true;
for (String string : strings) {
if (isNotEmpty(string)) {
result = false;
break;
}
}
return result;
}
/**
* 判断给定的字符串数组中是否全部都不为null且不为空
*
* @param strings 给定的字符串数组
* @return 是否全部都不为null且不为空
*/
public static boolean isNotEmpty(String... strings) {
boolean result = true;
for (String string : strings) {
if (isEmpty(string)) {
result = false;
break;
}
}
return result;
}
/**
* 如果字符串是null或者空就返回""
*/
public static String filterEmpty(String string) {
return StringUtils.isNotEmpty(string) ? string : "";
}
/**
* 在给定的字符串中,用新的字符替换所有旧的字符
*
* @param string 给定的字符串
* @param oldchar 旧的字符
* @param newchar 新的字符
* @return 替换后的字符串
*/
public static String replace(String string, char oldchar, char newchar) {
char chars[] = string.toCharArray();
for (int w = 0; w < chars.length; w++) {
if (chars[w] == oldchar) {
chars[w] = newchar;
break;
}
}
return new String(chars);
}
/**
* 把给定的字符串用给定的字符分割
*
* @param string 给定的字符串
* @param ch 给定的字符
* @return 分割后的字符串数组
*/
public static String[] split(String string, char ch) {
ArrayList