package com.gxever.mdp.core.utils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.List;
import java.util.regex.Pattern;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
public class StringUtil {
private static Logger logger = Logger.getLogger(StringUtil.class);
/**
* 判斷輸入的字符串是否全部為大寫
*
* @param str
* @return
*/
public static boolean testAllUpperCase(String str) {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c >= 97 && c <= 122) {
return false;
}
}
return true;
}
/**
* 替換路徑中的空格為%20
*
* @param str
* @return
*/
public static String replaceSpace(StringBuffer str) {
for (int k = 0; k < str.length(); k++) {
char index = str.charAt(k);
if (index == ' ') {
str.replace(k, k + 1, "%20");
}
}
return str.toString();
}
/**
* MD5加密
*
* @param str 加密字符串
* @return
*/
public static String md5(String str) {
String ret = "";
try {
ret = DigestUtils.md5Hex(str);
} catch (Exception e) {
logger.error("md5 error, exception:", e);
}
return ret;
}
/**
* 判斷浮點數整數 int 類型
*
* @param str
* @return
*/
public static boolean isInteger(String str) {
if (null == str || "".equals(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
/**
* 判斷浮點數(double和float)
*/
public static boolean isDouble(String str) {
if (null == str || "".equals(str)) {
return false;
}
Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
return pattern.matcher(str).matches();
}
public static int getInt(double number) {
BigDecimal bd = new BigDecimal(number).setScale(0, BigDecimal.ROUND_HALF_UP);
return Integer.parseInt(bd.toString());
}
/**
* 給第字符串第一個字母大寫
*
* @param str
* @return
*/
public static String capitalize(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return str;
}
return new StringBuilder(strLen)
.append(Character.toTitleCase(str.charAt(0)))
.append(str.substring(1))
.toString();
}
/**
* 移除樣式元素
*
* @param str
* @return
*/
public static String removeStyleEle(String str) {
if (str.contains("<font style=\"vertical-align: inherit;\">")) {
str = str.replaceAll("<font style=\"vertical-align: inherit;\">", "");
}
if (str.contains("</font>")) {
str = str.replaceAll("</font>", "");
}
return str;
}
/**
* 去除字符串中所包含的空格(包括:空格(全角,半角)、制表符、換頁符等)
*
* @param s
* @return
*/
public static String removeAllBlank(String s) {
String result = "";
if (null != s && !"".equals(s)) {
result = s.replaceAll("[ *| *| *|//s*]*", "");
}
return result;
}
/**
* 去除字符串中所包含的空格(包括:空格(全角,半角,制表符))
*
* @param s
* @return
*/
public static String trim(String s) {
String result = "";
if (null != s && !"".equals(s)) {
result = s.replaceAll(" ", "").replaceAll(" ", "").replaceAll("\t", "");
}
return result;
}
/**
* 去除字符串中 首尾 所包含的空格(包括:空格(全角,半角,制表符))
*
* @param s
* @return
*/
public static String trimHF(String s) {
s = s.trim();
while (s.startsWith(" ")) {//這里判斷是不是全角空格
s = s.substring(1, s.length()).trim();
}
while (s.endsWith(" ")) {
s = s.substring(0, s.length() - 1).trim();
}
while (s.startsWith(" ")) {//這里判斷是不是全角空格
s = s.substring(1, s.length()).trim();
}
while (s.endsWith(" ")) {
s = s.substring(0, s.length() - 1).trim();
}
return s;
}
/**
* 如果字符串包含%和_則需要轉義\%和\_--mysql查詢包含百分號的時候用
*
* @param s
* @return
*/
public static String replaceMySQLKeyWords(String s) {
String result = "";
if (null != s && !"".equals(s)) {
result = s.replaceAll("%", "\\\\%").replaceAll("_", "\\\\_");
}
return result;
}
/**
* 截取一級域名
*
* @param urlStr 網址(路徑)
* @return
*/
public static String cutOutUrl(String urlStr) {
String msg = "";
String[] str = {".com.cn/", ".gov.cn/", ".edu.cn/", ".com.cn", ".com.", ".com/", ".cn/", ".com", ".cn", ".net", ".biz", ".org", ".info", ".tv", ".mil", ".pro", ".coop"};
try {
if (StringUtils.isNotBlank(urlStr)) {
for (int i = 0; i < str.length; i++) {
if (urlStr.indexOf(str[i]) != -1) {
String substring = "";
substring = urlStr.substring(0, urlStr.indexOf(str[i]));
if (substring.indexOf(".") > 1) {
substring = substring.substring(urlStr.substring(0, urlStr.indexOf(str[i])).lastIndexOf(".")).replace(".", "");
} else {
msg = substring;
}
msg = substring + str[i];
if (StringUtils.isNotBlank(msg)) {
// 如果連接存在斜杠就去掉
if (msg.indexOf("/") != -1) {
msg = msg.substring(0, msg.length() - 1);
}
logger.info("cutOutUrl 截取后的結果url=======>>>> " + msg);
break;
}
}
}
}
} catch (Exception e) {
logger.error("cutOutUrl error", e);
}
return msg;
}
/**
* list 里面包含 一個字符串
*
* @param list
* @param str
* @return true:包含; false:不包含
*/
public static boolean listInclude(List<String> list, String str) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).indexOf(str) != -1) {
return true;
}
}
return false;
}
/**
* 用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;
}
/**
* 判斷給定的字符串數組中的所有字符串是否都為null或者是空的
* @param strings 給定的字符串
*/
public static boolean isEmpty(String... strings) {
boolean result = true;
for (String string : strings) {
if (isNotEmpty(string)) {
result = false;
break;
}
}
return result;
}
}