package com.example.utils.utils;
import java.text.DecimalFormat;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
/**
* @ClassName StringUtil
* @Description TODO
* @Author wushaopei
* @Date 2019/7/22 11:28
* @Version 1.0
*/
public class StringUtil {
/**逗号*/
public static final String UNDERLINE_DH = ",";
/**中文逗号*/
public static final String UNDERLINE_ZWDH = ",";
/**竖线*/
public static final String UNDERLINE_SX = "|";
/**下划线*/
public static final String UNDERLINE_XHX = "_";
/**空格*/
public static final String UNDERLINE_KG = " ";
/**冒号*/
public static final String UNDERLINE_MH = ":";
/**英文 点*/
public static final String UNDERLINE_D = ".";
/**英文 横杠*/
public static final String UNDERLINE_HG = "-";
/**英文 x*/
public static final String UNDERLINE_X = "x";
/**英文 斜杠/*/
public static final String UNDERLINE_XG = "/";
/**英文 单字符 逗号,/*/
public static final char UNDERLINE_CHAR_DH = ',';
/**英文转义 点*/
public static final String UNDERLINE_ESCAPE_D = "\\.";
/**转义竖线*/
public static final String UNDERLINE_ESCAPE_SX = "\\|";
public static final String ESCAPE_SX = "\\|";
public static final String SX = "|";
public static final String HG = "-";
public static final String KC = "";
public static final String MH = ":";
public static final String KG = " ";
public static final String DH = ",";
/**红波*/
public static final String HONGBO_ZW_STRING = "红波";
/**蓝波*/
public static final String LANBO_ZW_STRING = "蓝波";
/**绿波*/
public static final String LVBO_ZW_STRING = "绿波";
/** 鼠 */
public static final String SHU_ZW_STRING = "鼠";
/** 牛 */
public static final String NIU_ZW_STRING = "牛";
/** 虎 */
public static final String HU_ZW_STRING = "虎";
/** 兔 */
public static final String TU_ZW_STRING = "兔";
/** 龙 */
public static final String LONG_ZW_STRING = "龙";
/** 蛇 */
public static final String SHE_ZW_STRING = "蛇";
/** 马 */
public static final String MA_ZW_STRING = "马";
/** 羊 */
public static final String YANG_ZW_STRING = "羊";
/** 猴 */
public static final String HOU_ZW_STRING = "猴";
/** 鸡 */
public static final String JI_ZW_STRING = "鸡";
/** 狗 */
public static final String GOU_ZW_STRING = "狗";
/** 猪 */
public static final String ZHU_ZW_STRING = "猪";
/** 自主开奖的map对应的key值*/
//最低中奖金额
public static final String MIN_WIN_AMOUNT = "minWinMoney";
/** 金流方向数据字典对应的key值*/
/**为盈利的类型 */
public static final String ACCOUNT_DETAILS_POROFIT_DICT_KEY = "41,12,13,92,93,81,14,83,112,113";
/**为亏损的类型 */
public static final String ACCOUNT_DETAILS_LOSS_DICT_KEY = "71,72,73,74,75,21,31,51,91,82,15,16,17,61,94,111,76";
/**
* 首字母小写
*
* @return String
* @throws
*/
public static String firstToLowerCase(String str) {
if (str != null && str.length() != 0) {
return Character.toLowerCase(str.charAt(0)) + str.substring(1);
}
return str;
}
/**
* 首字母大些
*
* @return String
* @throws
*/
public static String firstToUpperCase(String str) {
if (str != null && str.length() != 0) {
return Character.toUpperCase(str.charAt(0)) + str.substring(1);
}
return str;
}
/**
* 将页面或PO字段转换成全大写并以下划线分隔的数据库字段
*
* @param param 例如: custName
* @return 换换后字符串 CUST_NAME
*/
public static String capitalConvertUnderline(String dtoName) {
StringBuffer sub = null;
if (isNotBlank(dtoName)) {
char[] ch = dtoName.toCharArray();
sub = new StringBuffer();
String temp = null; //
for (int i = 0; i < dtoName.length(); i++) {
temp = String.valueOf(ch[i]);
if (StringUtil.isNotBlank(temp)
&& Character.isUpperCase(ch[i])) {
sub.append(UNDERLINE_XHX + ch[i]);
} else {
sub.append(ch[i]);
}
}
return sub.toString().toUpperCase();
}
return null;
}
/**
* 将页面或PO字段转换成以下划线分隔的数据库字段
*
* @param param 例如: custName
* @return 换换后字符串 CUST_NAME
*/
public static String camelToUnderline(String param) {
if (param == null || "".equals(param.trim())) {
return "";
}
int len = param.length();
StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = param.charAt(i);
if (Character.isUpperCase(c)) {
sb.append(UNDERLINE_XHX);
sb.append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* 判断字符串是否是null或者""
*
* @param str
* @return
*/
public static boolean isBlank(String str) {
if (str == null) {
return true;
} else if (str.length() == 0) {
return true;
} else {
return false;
}
}
/**
* 判断字符串是否不为空
*
* @param str
* @return
*/
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
/**
* 用分隔符expr 分割str字符串,返回string[]数组
*
* @param str
* :要分割的字符串
* @param expr
* :分隔符
* @return String[]
*/
public static String[] stringToArray(String str, String expr) {
return str.split(expr);
}
/**
* 将字符串数组以分隔符方式返回字符串
*
* @param arr
* @param expr
* @return
*/
public static String arrayToString(String[] arr, String expr) {
String strInfo = "";
if (arr != null && arr.length > 0) {
StringBuffer sf = new StringBuffer();
for (String str : arr) {
sf.append(str);
sf.append(expr);
}
strInfo = sf.substring(0, sf.length() - 1);
}
return strInfo;
}
/**
* 将list集合里字符串用expr分隔符连接,返回字符串
*
* @param arr
* @param expr
* @return
*/
public static String listToString(List<String> list, String expr) {
String strInfo = "";
if (list != null && list.size() > 0) {
StringBuffer sf = new StringBuffer();
for (String str : list) {
sf.append(str);
sf.append(expr);
}
strInfo = sf.substring(0, sf.length() - 1);
}
return strInfo;
}
/**
* 屏蔽相关信息
*
* @param arr
* @param expr
* @return
*/
public static String shieldAll(String str) {
str = shieldEmail(str);
str = shieldMobile(str);
str = shieldTelephone(str);
str = shieldQQ(str);
return str;
}
/**
* 屏蔽EMAIL
*
* @param email
* @return
*/
public static String shieldEmail(String email) {
if (!email.isEmpty()) {
String emailRegEx = "([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)";
Pattern emailPattern = Pattern.compile(emailRegEx);
Matcher emailMatcher = emailPattern.matcher(email);
while (emailMatcher.find()) {
String oldSubStr = emailMatcher.group(); // 获得邮箱地址
int location = oldSubStr.indexOf("@");
String temp = oldSubStr.substring(0, location); // 获得邮箱账号
String newSubStr = oldSubStr.replace(temp, "****"); // 用星号替代邮箱账号
email = email.replace(oldSubStr, newSubStr); // 替换补充说明中的邮箱地址
}
}
return email;
}
/**
* 屏蔽手机号码
*
* @param mobile
* @return
*/
public static String shieldMobile(String mobile) {
if (!mobile.isEmpty()) {
String mobileRegEx = "((13[0-9])|(15[0-9])|(18[0-9])|(17[0-9]))\\d{8}";
Pattern mobilePattern = Pattern.compile(mobileRegEx);
Matcher mobileMatcher = mobilePattern.matcher(mobile);
while (mobileMatcher.find()) {
String oldSubStr = mobileMatcher.group(); // 获得手机号码
String temp = oldSubStr.substring(3, oldSubStr.length()); // 截取手机号码后八位
String newSubStr = oldSubStr.replace(temp, "********"); // 用星号替代手机号码后八位
mobile = mobile.replace(oldSubStr, newSubStr); // 替换补充说明中的手机号码
}
}
return mobile;
}
/**
* 屏蔽座机号码
*
* @param telephone
* @return
*/
public static String shieldTelephone(String telephone) {
if (!telephone.isEmpty()) {
String telephoneRegEx = "(0[0-9]{2,3})?[-| ]([2-9][0-9]{6,7})+(-[0-9]{1,4})?";
Pattern telephonePattern = Pattern.compile(telephoneRegEx);
Matcher telephoneMatcher = telephonePattern.matcher(telephone);
while (telephoneMatcher.find()) {
String oldSubStr = telephoneMatcher.group(); // 获得座机号码
String temp = oldSubStr.substring(5, oldSubStr.length()); // 截取座机号码后7位
String newSubStr = oldSubStr.replace(temp, "*******"); // 用星号替代座机号码后7位
telephone = telephone.replace(oldSubStr, newSubStr); // 替换补充说明中的座机号码
}
}
return telephone;
}
/**
* 屏蔽QQ
*
* @param telephone
* @return
*/
public static String shieldQQ(String qq) {
if (!qq.isEmpty()) {
String qqRegEx = "[1-9][0-9]{5,9}";
Pattern qqPattern = Pattern.compile(qqRegEx);
Matcher qqMatcher = qqPattern.matcher(qq);
while (qqMatcher.find()) {
String oldSubStr = qqMatcher.group(); // 获得QQ号码
String temp = oldSubStr.substring(2, oldSubStr.length()); // 截取QQ号码
String newSubStr = oldSubStr.replace(temp, "******"); // 用星号替代
qq = qq.replace(oldSubStr, newSubStr); // 替换补充说明中的QQ号码
}
}
return qq;
}
public static String toGb2312(String str) {
if (str == null) {
return null;
}
String retStr = str;
byte[] b;
try {
b = str.getBytes("ISO8859_1");
for (int i = 0; i < b.length; i++) {
byte b1 = b[i];
if (b1 == 63) {
break; // 1
} else if (b1 > 0) {
continue;// 2
} else if (b1 < 0) { // 不可能为0,0为字符串结束符
// 小于0乱码
retStr = new String(b, "GB2312");
break;
}
}
} catch (UnsupportedEncodingException e) {
// e.printStackTrace();
}
return retStr;
}
/**
* 判断字符是否是中文
*
* @param c
* 字符
* @return 是否是中文
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
/**
* 判断字符串是否是乱码
*
* @param strName
* 字符串
* @return 是否是乱码
*/
public static boolean isMessyCode(String strName) {
Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
Matcher m = p.matcher(strName);
String after = m.replaceAll("");
String temp = after.replaceAll("\\p{P}", "");
char[] ch = temp.trim().toCharArray();
float chLength = ch.length;
float count = 0;
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (!Character.isLetterOrDigit(c)) {
if (!isChinese(c)) {
count = count + 1;
}
}
}
float result = count / chLength;
if (result > 0.4) {
return true;
} else {
return false;
}
}
public static boolean validateMobile(String mobile) {
if(StringUtils.isEmpty(mobile)) {
return false;
}
String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
Pattern pMobile = Pattern.compile(REGEX_MOBILE);
return pMobile.matcher(mobile).matches();
}
public static boolean validateEmail(String email) {
if(StringUtils.isEmpty(email)) {
return false;
}
String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern pEmail = Pattern.compile(REGEX_EMAIL);
return pEmail.matcher(email).matches();
}
/**
* 判断是否为数字
*
* @param object
* @return
*/
public static boolean isNumber(Object object) {
if (object == null) {
return false;
}
String value = object.toString();
return value.matches("\\d+");
}
/**
* 计算采用编码方式时字符串所占字节数
*
* @param content
* @return
*/
public static int getByteSize(String encode, String content) {
int size = 0;
if (null != content) {
try {
// 汉字采用utf-8编码时占3个字节
size = content.getBytes(encode).length;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return size;
}
public static String objToString(Object obj) {
if (obj != null) {
return obj.toString();
} else {
return "";
}
}
/**
* 版本号只加一个版本,逢9进1
*
* @author administrator
* @time 2017年5月23日
*
* @param arg
* @return
*/
public static String versionIncre(String arg) {
String[] argAry = arg.split(UNDERLINE_ESCAPE_D);
for (int i = argAry.length-1; i > -1; i--) {
int num = Integer.valueOf(argAry[i]);
if (num == 9) {
argAry[i] = i == 0 ? (num + 1) + "" : "0";
}else {
argAry[i] = (num + 1) + "";
break;
}
}
return StringUtils.join(argAry, UNDERLINE_D);
}
/**
* 将double转成非科学计数法的字符串
* @param Double
* @return
*/
public static String formatFloatNumber(Double value) {
if(value != null){
if(value.doubleValue() != 0.00){
java.text.DecimalFormat df = new java.text.DecimalFormat("########.00");
return df.format(value.doubleValue());
}else{
return "0.00";
}
}
return "";
}
public static boolean validateQQ(String qq) {
if(StringUtils.isEmpty(qq)) {
return false;
}
String REGEX_QQ = "[1-9][0-9]{4,14}";
Pattern pQQ = Pattern.compile(REGEX_QQ);
return pQQ.matcher(qq).matches();
}
public static String setAgentCodeToOrderNo(String orderNo, String agentCode) {
return orderNo+"zzz"+agentCode;
}
public static String setAgentNumToOrderNo(Integer num, String orderNo) {
return num+orderNo;
}
public static String getOrderNoByOrder(String orderNo) {
String ss = orderNo.substring(3, orderNo.length());
return ss;
}
public static String getNumByOrder(String orderNo) {
String ss = orderNo.substring(0, 3);
return ss;
}
public static String getAgentCodeByOrderNo(String orderNoZZZagentCode) {
String[] ss = orderNoZZZagentCode.split("zzz");
return ss[1];
}
public static String getOrderNoByOrderNo(String orderNoZZZagentCode) {
String[] ss = orderNoZZZagentCode.split("zzz");
return ss[0];
}
/**
*
* 将String转换成unicode编码格式
* @param str
* @return String
* @throws
*/
public static String unicodeEncoding(String str) {
if (str == null||str.trim().length()==0) {
return str;
}
StringBuffer unicodeBytes = new StringBuffer();
for (int byteIndex = 0; byteIndex < str.length(); byteIndex++) {
String hexB = Integer.toHexString(str.charAt(byteIndex));
unicodeBytes.append("\\u");
if (hexB.length() <= 2) {
unicodeBytes.append("00");
}
unicodeBytes.append(hexB);
}
return unicodeBytes.toString();
}
/**
* 将unicode转中文
* @param dataStr
* @return
*/
public static String decodeUnicode(String dataStr) {
int start = 0;
int end = 0;
StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("\\u", start + 2);
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
} else {
charStr = dataStr.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}
/**
* 数字类型强转,如果是小数类型保留两位小数
* @param v
* @return
*/
@SuppressWarnings("unchecked")
public static String castNumber(Object v){
DecimalFormat df = new DecimalFormat("######0.00");
String s = df.format(v);
return s;
}
//main 方法测试 编码、解码 结果
public static void main(String[] args) {
System.out.println(unicodeEncoding("康娜酱"));
String s = unicodeEncoding("康娜酱");
String s1 = decodeUnicode(s);
System.out.println(s1);
}
}
main方法测试结果:
\u5eb7\u5a1c\u9171
康娜酱