stringUtil 工具类


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;
}

}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM