1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393 |
- package com.mixsmart.utils;
- import com.bstek.ureport.console.designer.ReportUtils;
- import com.jfirer.baseutil.encrypt.Md5Util;
- import com.mixsmart.constant.IMixConstant;
- import com.mixsmart.exception.NullArgumentException;
- import org.springblade.core.tool.utils.StringPool;
- import java.io.UnsupportedEncodingException;
- import java.lang.reflect.Field;
- import java.lang.reflect.Modifier;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- /**
- * @version 1.0
- * @Description:字符串处理工具类
- * @author:rock
- * @time:2020年4月21日 下午12:05:04
- */
- public class StringUtils {
- private StringUtils() {
- throw new UnsupportedOperationException("StringUtils类无法实例化");
- }
- private static final String CHARSET_NAME = "UTF-8";
- /**
- * 判断是否为空
- *
- * @param value 内容
- * @return 为空返回:true;否则返回:false
- */
- public static boolean isEmpty(String value) {
- return (null == value || value.trim().length() == 0);
- }
- /**
- * @return boolean
- * @Description 批量判读为空
- * @Param [values]
- * @Author yangyj
- * @Date 2021.11.05 13:42
- **/
- public static boolean isEmpty(Object... values) {
- for (Object value : values) {
- if (!isEmpty(value)) {
- return false;
- }
- }
- return true;
- }
- /**
- * 判断是否不为空
- *
- * @param value 内容
- * @return 不为空返回:true;否则返回:false
- */
- public static boolean isNotEmpty(String value) {
- return !isEmpty(value);
- }
- /**
- * 判断参数;如果参数<code>value</code> 为空;
- * 则抛出参数为空异常(运行时异常)
- *
- * @param value 需要判断的参数
- * @param msg 提示信息
- */
- public static void isAssert(Object value, String msg) {
- msg = isEmpty(msg) ? "提供的参数为空" : msg;
- if (null == value) {
- throw new NullArgumentException(msg);
- }
- if (value instanceof String) {
- if (isEmpty(value.toString())) {
- throw new NullArgumentException(msg);
- }
- } else if (value instanceof Collection) {
- if (CollectionUtils.isEmpty((Collection<?>) value)) {
- throw new NullArgumentException(msg);
- }
- } else if (value.getClass().isArray()) {
- if (ArrayUtils.isEmpty((Object[]) value)) {
- throw new NullArgumentException(msg);
- }
- }
- }
- /**
- * 判断值是否相等
- *
- * @param value1
- * @param value2
- * @return 相等返回:true;否则返回:false
- */
- public static boolean isEquals(Object value1, Object value2) {
- boolean is = false;
- if (null != value1 && null != value2) {
- is = value1.toString().equals(value2.toString());
- } else if (null == value1 && null == value2) {
- is = true;
- }
- return is;
- }
- /**
- * 判断值是否不相等
- *
- * @param value1
- * @param value2
- * @return 相等返回:true;否则返回:false
- */
- public static boolean isNotEquals(Object value1, Object value2) {
- return !isEquals(value1, value2);
- }
- /**
- * 判断值是否相等(不区分大小写)
- *
- * @param value1
- * @param value2
- * @return 相等返回:true;否则返回:false
- */
- public static boolean isEqualsIgnoreCase(Object value1, Object value2) {
- boolean is = false;
- if (null != value1 && null != value1) {
- is = value1.toString().equalsIgnoreCase(value2.toString());
- } else if (null == value1 && null == value1) {
- is = true;
- }
- return is;
- }
- /**
- * 判断值是否不相等(不区分大小写)
- *
- * @param value1
- * @param value2
- * @return 相等返回:true;否则返回:false
- */
- public static boolean isNotEqualsIgnoreCase(Object value1, Object value2) {
- return !isEqualsIgnoreCase(value1, value2);
- }
- /**
- * null转换为“”
- *
- * @param obj
- * @return 返回处理后的结果
- */
- public static String handleNull(Object obj) {
- if (null == obj) {
- return "";
- } else {
- return obj.toString().trim();
- }
- }
- /**
- * 当值为null转化为“null”
- *
- * @param obj
- * @return 返回处理后的结果
- */
- public static String nullToStr(Object obj) {
- if (null == obj) {
- return "null";
- } else {
- return obj.toString().trim();
- }
- }
- /**
- * 对象转化为整型
- * <p>注:该方法已过时,请用{@link #handleObj2Integer(Object)} 代替</p>
- *
- * @param obj
- * @return 返回转化结果
- */
- public static Integer handObj2Integer(Object obj) {
- return handleObj2Integer(obj);
- }
- /**
- * 对象转化为整型
- *
- * @param obj
- * @return 返回转化结果
- */
- public static Integer handleObj2Integer(Object obj) {
- if (null == obj) {
- return 0;
- } else {
- double value = 0;
- try {
- value = Double.parseDouble(obj.toString());
- } catch (Exception ex) {
- value = 0;
- }
- return (int) value;
- }
- }
- /**
- * @return java.lang.Double
- * @Description 对象转Double
- * @Param [obj, scale]
- * @Author yangyj
- * @Date 2022.04.20 14:15
- **/
- public static Double obj2Double(Object obj, Object scale) {
- if (StringUtils.isNumber(obj)) {
- if (StringUtils.isNumber(scale)) {
- obj = new BigDecimal(StringUtils.number2String(obj, scale));
- }
- BigDecimal big = new BigDecimal(obj.toString());
- return big.doubleValue();
- }
- return null;
- }
- public static Double obj2Double(Object obj) {
- return obj2Double(obj, null);
- }
- /**
- * 数字null转换为“0”
- *
- * @param obj
- * @return 返回转化结果
- */
- public static String handleNumNull(Object obj) {
- if (null == obj) {
- return "0";
- } else {
- return obj.toString().trim();
- }
- }
- /**
- * 判断是否为数字(包括小数)
- *
- * @param value
- * @return 数字返回:true;否则返回:false
- */
- public static boolean isNum(Object value) {
- boolean is = false;
- if (value != null) {
- Pattern pattern = Pattern.compile("\\d+|\\d+\\.\\d+");
- Matcher matcher = pattern.matcher(value.toString());
- if (matcher.matches()) {
- is = true;
- } else {
- is = false;
- }
- }
- return is;
- }
- /**
- * 判断是否数字整数
- *
- * @param value
- * @return 是返回:true;否则返回:false
- */
- public static boolean isInteger(String value) {
- boolean is = false;
- Pattern pattern = Pattern.compile("\\d+");
- if (null != value && value.length() > 1) {
- pattern = Pattern.compile("^[1-9]\\d+");
- }
- Matcher matcher = pattern.matcher(value);
- if (matcher.matches()) {
- is = true;
- } else {
- is = false;
- }
- return is;
- }
- /**
- * 判断是否小数
- *
- * @param value
- * @return 是返回:true;否则返回:false
- */
- public static boolean isDecimal(String value) {
- boolean is = false;
- Pattern pattern = Pattern.compile("\\d+\\.\\d+");
- Matcher matcher = pattern.matcher(value);
- if (matcher.matches()) {
- is = true;
- } else {
- is = false;
- }
- return is;
- }
- /**
- * 随机生成数
- *
- * @param num 要生成随机数的个数
- * @return 返回随机生成数
- */
- public static String randomNum(int num) {
- Random random = new Random();
- String numStr = "";
- for (int i = 0; i < num; i++) {
- numStr += random.nextInt(10);
- }
- return numStr;
- }
- /**
- * 按日期格式生成序列号
- *
- * @param dateFormaterStr 日期格式
- * @return 返回序列号
- */
- public static String createSerialNum(String dateFormaterStr) {
- String serialNum = null;
- if (isNotEmpty(dateFormaterStr)) {
- SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormaterStr);
- serialNum = dateFormatter.format(new Date());
- dateFormatter = null;
- }
- return serialNum;
- }
- /**
- * 生成UUID序列号
- *
- * @return 返回UUID
- */
- public static String uuid() {
- String value = UUID.randomUUID().toString();
- value = value.replaceAll("-", "");
- return value.substring(0, 28);
- }
- public static String uuid(int max) {
- String value = UUID.randomUUID().toString();
- value = value.replaceAll("-", "");
- return value.substring(0, max);
- }
- /**
- * 获取文件后缀
- *
- * @param fileName
- * @return 返回文件后缀
- */
- public static String getFileSuffix(String fileName) {
- int index = fileName.lastIndexOf(".");
- if (index != -1) {
- return fileName.substring(index + 1);
- }
- return "";
- }
- /**
- * 去掉文件后缀
- *
- * @param fileName
- * @return 返回文件后缀
- */
- public static String trimFileSuffix(String fileName) {
- return fileName.substring(0, fileName.lastIndexOf("."));
- }
- /**
- * 验证手机号码
- *
- * @param phoneNo
- * @return 验证成功返回:true;否则返回:false
- */
- public static boolean isPhoneNO(String phoneNo) {
- Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
- Matcher m = p.matcher(phoneNo);
- return m.matches();
- }
- /**
- * 验证固定电话号码
- *
- * @param tel
- * @return 验证成功返回:true;否则返回:false
- */
- public static boolean isFixedTelephone(String tel) {
- Pattern p = Pattern.compile("^[0][0-9]{2,3}-[2-9][0-9]{6,7}(-[0-9]{1,4})?");
- Matcher m = p.matcher(tel);
- return m.matches();
- }
- /**
- * 验证匿名
- *
- * @param anonymous
- * @return 验证成功返回:true;否则返回:false
- */
- public static boolean isAnonymous(String anonymous) {
- Pattern p = Pattern.compile("^[\\u4e00-\\u9fa5|A-Za-z]([\\w|\\u4e00-\\u9fa5]){1,7}$");
- Matcher m = p.matcher(anonymous);
- return m.matches();
- }
- /**
- * 验证汉字
- *
- * @param value
- * @return 验证成功返回:true;否则返回:false
- */
- public static boolean isChinese(String value) {
- Pattern p = Pattern.compile("^[\\u4E00-\\u9FFF]+$");
- Matcher m = p.matcher(value);
- boolean is = m.matches();
- return is;
- }
- /**
- * 验证正则表达式
- *
- * @param value
- * @return 验证成功返回:true;否则返回:false
- */
- public static boolean isCheckRegex(String value, String regex) {
- Pattern p = Pattern.compile(regex);
- Matcher m = p.matcher(value);
- boolean is = m.matches();
- return is;
- }
- /**
- * 验证QQ号码
- *
- * @param value
- * @return 验证成功返回:true;否则返回:false
- */
- public static boolean isQQ(String value) {
- Pattern p = Pattern.compile("^[1-9]\\d{6,11}$");
- Matcher m = p.matcher(value);
- return m.matches();
- }
- /**
- * 验证email
- *
- * @param email
- * @return 验证成功返回:true;否则返回:false
- */
- public static boolean isEmail(String email) {
- Pattern p = Pattern.compile("^([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\\_|\\.]?)*[a-zA-Z0-9]+\\.[a-zA-Z]{2,3}$");
- Matcher m = p.matcher(email);
- return m.matches();
- }
- /**
- * 秒转化为:HH:mm:SS格式
- *
- * @param second
- * @return 返回转化后的结果
- */
- public static String secondToHHMMSS(long second) {
- long h = 0, m = 0, s = 0, tmp = 0;
- if (second >= 3600) {
- h = second / 3600;
- tmp = second % 36000;
- if (tmp >= 60) {
- m = tmp / 60;
- s = tmp % 60;
- } else {
- s = tmp;
- }
- } else if (second >= 60) {
- m = second / 60;
- s = second % 60;
- } else {
- s = second;
- }
- return (h > 9 ? h : "0" + h) + ":" + (m > 9 ? m : "0" + m) + ":" + (s > 9 ? s : "0" + s);
- }
- /**
- * 过滤特殊字符
- *
- * @param params
- * @return 返回过滤后的结果
- */
- public static String filterSQLParams(String params) {
- if (!isEmpty(params)) {
- StringBuilder strBuilder = new StringBuilder();
- strBuilder.append("'|\"|update|delete|select|drop|insert|=|;|0x|\\(|\\)|\\s|\\*|\\?|\\%|\\$");
- strBuilder.append("|and|exec|execute|chr|mid|master|truncate|char|declare|sitename|net user|xp_cmdshell|or");
- strBuilder.append("|\\+|,|like'|table|from|grant|use|group_concat|column_name|information_schema.columns");
- strBuilder.append("|table_schema|union|where|order|by|count");
- strBuilder.append("|--|,|like|//|/|#");
- String params1 = params.toLowerCase();
- params1 = params1.replaceAll(strBuilder.toString(), "");
- if ("".equals(params1) && !"''".equals(params)) {
- params = params1;
- }
- params = params.replaceAll("&", "&");
- params = params.replaceAll("<", "<");
- params = params.replaceAll(">", ">");
- }
- return params;
- }
- /**
- * 根据regex分隔字符串
- * 然后用逗号","重组
- *
- * @param ids
- * @param regex
- * @return 返回处理后的结果
- */
- public static String splitIds(String ids, String regex) {
- String newIds = "";
- if (isNotEmpty(ids)) {
- String[] idsArr = ids.split(regex);
- for (int i = 0; i < idsArr.length; i++) {
- if (i != (idsArr.length - 1)) {
- newIds += "'" + idsArr[i] + "',";
- } else {
- newIds += "'" + idsArr[i] + "'";
- }
- }
- }
- return newIds;
- }
- /**
- * 计算出文件大小
- *
- * @param size
- * @return 返回处理后的结果;<br />
- * 格式为:"100 KB"或”100 M“或”100 G“
- */
- public static String fileSize(long size) {
- DecimalFormat df = new DecimalFormat("0.0#");
- long KB = 1024;
- long MB = KB * 1024;
- long GB = MB * 1024;
- String valueStr = null;
- if (size < 0) {
- valueStr = "0 KB";
- } else if (size < KB * 1024) {
- double value = (double) size / KB;
- valueStr = df.format(value) + " KB";
- } else if (size < MB * 1024) {
- double value = (double) size / MB;
- valueStr = df.format(value) + " M";
- } else {
- double value = (double) size / GB;
- valueStr = df.format(value) + " G";
- }
- df = null;
- return valueStr;
- }
- /**
- * 过滤目录结构(防止参数传递目录结构)
- *
- * @param value
- * @return 返回过滤后的结果
- */
- public static String filterFilePath(String value) {
- if (!StringUtils.isEmpty(value)) {
- value.replaceAll("\\.|/|\\\\\\\\|\\\\|:|%2F|%2E|25%|20%|%5C|60%|27%|%3A|%2A", "");
- }
- return value;
- }
- /**
- * 判断是否含有contain指定的值
- *
- * @param value
- * @param contain
- * @return 包含返回:true;否则返回:false
- */
- public static boolean isContains(String value, String contain) {
- boolean is = false;
- if (!isEmpty(value) && null != contain) {
- is = value.contains(contain) ? true : false;
- }
- return is;
- }
- /**
- * list转换为数组
- *
- * @param values
- * @return 返回转化结果
- */
- public static String[] list2Array(Collection<String> values) {
- String[] valueArray = null;
- if (null != values && values.size() > 0) {
- valueArray = new String[values.size()];
- values.toArray(valueArray);
- }
- return valueArray;
- }
- /**
- * 验证IP地址
- *
- * @param ip
- * @return 验证成功返回:true;否则返回:false
- */
- public static boolean checkIp(String ip) {
- boolean is = false;
- String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
- + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
- + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
- + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(ip);
- if (matcher.matches()) {
- is = true;
- }
- return is;
- }
- /**
- * 集合转换为字符串,字符串之间用separater提供的参数分隔
- *
- * @param values
- * @param separater
- * @return 返回处理后的结果
- */
- public static <T> String collection2String(Collection<T> values, String separater) {
- StringBuilder strBuilder = null;
- if (null != values && values.size() > 0) {
- strBuilder = new StringBuilder();
- if (isEmpty(separater)) {
- separater = "";
- }
- for (T value : values) {
- strBuilder.append(StringUtils.handleNull(value) + separater);
- }
- if (isNotEmpty(separater)) {
- strBuilder.delete(strBuilder.length() - 1, strBuilder.length());
- }
- }
- return strBuilder != null ? strBuilder.toString() : null;
- }
- /**
- * 替换多个空格、换行、回车、tab符等为一个空格
- *
- * @param value 预处理的字符串
- * @return 返回处理后的字符串
- */
- public static String removeMultiSpace(String value) {
- if (StringUtils.isNotEmpty(value)) {
- value = value.replaceAll("\\s+|\n|\r|\n|\t", " ");
- }
- return value;
- }
- /**
- * 替换特殊字符
- *
- * @param value 预处理的字符串
- * @return 返回处理后的结果
- */
- public static String repaceSpecialChar(String value) {
- if (isNotEmpty(value)) {
- value = value.replaceAll("\r|\n|\t", "");
- }
- return value;
- }
- public static String changeQuotation(String value) {
- if (isNotEmpty(value)) {
- value = value.replaceAll("'", "\\\\u0027");
- }
- return value;
- }
- /**
- * 替换斜杠
- *
- * @param value
- * @return 返回处理后的结果
- */
- public static String repaceSlash(String value) {
- if (isNotEmpty(value)) {
- //value = value.replaceAll("\\\\", "\\\\\\\\\\\\\\\\");
- value = value.replaceAll("\\\\", "\\\\\\\\");
- value = value.replaceAll("`", "\\\\`");
- value = value.replaceAll("'", "\\\\`");
- }
- return value;
- }
- /**
- * 替换斜杠
- *
- * @param value
- * @return 返回处理后的结果
- */
- public static String repaceSlashCopyTable(String value) {
- if (isNotEmpty(value)) {
- //value = value.replaceAll("\\\\", "\\\\\\\\\\\\\\\\");
- //value = value.replaceAll("\\\\", "\\\\\\\\");
- value = value.replaceAll("`", "\\\\`");
- }
- return value;
- }
- /**
- * 去空格
- *
- * @param value
- * @return 返回处理后的结果
- */
- public static String repaceBlank(String value) {
- if (isNotEmpty(value)) {
- value = value.replaceAll("\\s", "");
- }
- return value;
- }
- /**
- * 字符串转换为list
- *
- * @param value 要转换的字符串
- * @param separater 分隔符
- * @return 返回转换后的List
- */
- public static List<String> string2List(String value, String separater) {
- List<String> lists = null;
- if (isNotEmpty(value)) {
- separater = isEmpty(separater) ? "," : separater;
- String[] array = value.split(separater);
- lists = Arrays.asList(array);
- }
- return lists;
- }
- /**
- * 字符串转换为Set
- *
- * @param value 要转换的字符串
- * @param separater 分隔符
- * @return 返回转换后的Set
- */
- public static Set<String> string2Set(String value, String separater) {
- Set<String> sets = null;
- if (isNotEmpty(value)) {
- separater = isEmpty(separater) ? "," : separater;
- String[] array = value.split(separater);
- sets = new HashSet<String>();
- sets.addAll(Arrays.asList(array));
- }
- return sets;
- }
- /**
- * 如果值为空“”转换为NULL
- *
- * @param value
- * @return
- */
- public static String empty2Null(String value) {
- return isEmpty(value) ? null : value;
- }
- /**
- * 移除多值,只获取第一个值;多值之间通过英文逗号分隔
- *
- * @param value 需要处理的值
- * @return 返回处理后的值
- */
- public static String removeMultiValue(String value) {
- if (isEmpty(value)) {
- return value;
- }
- int p = value.indexOf(IMixConstant.MULTI_VALUE_SPLIT);
- if (p >= 0) {
- value = value.substring(0, p);
- }
- return value;
- }
- /**
- * 数组转化为字符串;
- * 如果<code>separate</code>为空,则采用默认值;默认值为:{@link IMixConstant#MULTI_VALUE_SPLIT}
- *
- * @param objs 数组
- * @param separate 分隔符
- * @return 返回数组转化成功后的字符串;失败返回:null
- */
- public static String arrayToString(Object[] objs, String separate) {
- StringBuilder strBuff = null;
- if (null == objs || objs.length == 0) {
- return null;
- }
- if (StringUtils.isEmpty(separate)) {
- separate = IMixConstant.MULTI_VALUE_SPLIT;
- }
- strBuff = new StringBuilder();
- for (int i = 0; i < objs.length; i++) {
- if (i < objs.length - 1) {
- strBuff.append(String.valueOf(objs[i]) + separate);
- } else {
- strBuff.append(String.valueOf(objs[i]));
- }
- }//for
- objs = null;
- return (null != strBuff) ? strBuff.toString() : null;
- }
- /**
- * 字符串转化为数组;
- * 如果<code>separate</code>为空,则采用默认值;默认值为:{@link IMixConstant#MULTI_VALUE_SPLIT}
- *
- * @param value 原字符串
- * @param separate 分隔符
- * @return 返回字符串分割成功后的数组
- */
- public static String[] stringToArray(String value, String separate) {
- String[] array = null;
- if (isEmpty(separate)) {
- separate = IMixConstant.MULTI_VALUE_SPLIT;
- }
- if (isNotEmpty(value)) {
- array = value.split(separate);
- }
- value = null;
- return array;
- }
- /**
- * 按separate分离成数组,判断该数组里面是否包含subStr;
- * 如果<code>separate</code>为空,则采用默认值;默认值为:{@link IMixConstant#MULTI_VALUE_SPLIT}
- *
- * @param str 字符串
- * @param subStr 子字符串
- * @param separate 分隔符
- * @return 包含返回:true;否则返回:false
- */
- public static boolean isArrayContains(String str, String subStr, String separate) {
- if (isEmpty(str)) {
- return isEmpty(subStr);
- }
- if (null == subStr) {
- return true;
- }
- boolean is = false;
- if (isEmpty(separate)) {
- separate = IMixConstant.MULTI_VALUE_SPLIT;
- }
- String[] strArray = str.split(separate);
- for (int i = 0; i < strArray.length; i++) {
- if (subStr.equals(strArray[i].trim())) {
- is = true;
- break;
- }
- }//for
- return is;
- }
- /**
- * 判断对象是否为空,或转换为字符串后的值是否为空
- *
- * @param value 需要判断的值
- * @return 为空(null或“”)返回true;否则返回false
- */
- public static boolean isEmpty(Object value) {
- if (null == value) {
- return true;
- } else {
- return isEmpty(handleNull(value));
- }
- }
- /**
- * 判断对象是否不为空;
- *
- * @param value 需要判断的值
- * @return 如果不为空返回true;否则返回false
- */
- public static boolean isNotEmpty(Object value) {
- return !isEmpty(value);
- }
- /**
- * @return boolean
- * @Description 批量非空校验
- * @Param [values]
- * @Author yangyj
- * @Date 2021.10.19 17:04
- **/
- public static boolean isNotEmpty(Object... values) {
- for (Object value : values) {
- if (isEmpty(value)) {
- return false;
- }
- }
- return true;
- }
- /**
- * @Description 是否数字
- * @Param [value]
- * @return boolean
- * @Author yangyj
- * @Date 2022.06.02 14:54
- **/
- public static boolean isNumber(Object value) {
- if (isEmpty(value)) {
- return false;
- }
- if (value instanceof Number) {
- return true;
- }
- String pattern = "^[+-]?\\d+(\\.[\\dEe]+)?$";
- Pattern r = Pattern.compile(pattern);
- Matcher m = r.matcher(String.valueOf(value));
- return m.matches();
- }
- public static boolean isDouble(Object value) {
- return isNumber(value) && value.toString().contains(".");
- }
- /**
- * @return java.util.Map<java.lang.String, java.lang.Object>
- * @Description
- * @Param [obj] 对象转成map集合
- * @Author yangyj
- * @Date 2021.05.27 14:33
- **/
- public static Map<String, Object> objectToMap(Object obj) {
- Map<String, Object> map = new HashMap<String, Object>();
- try {
- Class<?> clazz = obj.getClass();
- for (Field field : clazz.getDeclaredFields()) {
- field.setAccessible(true);
- if (Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) {
- continue;
- }
- String fieldName = field.getName();
- Object data = field.get(obj);
- Object toStrValue = handleNull(data);
- if (isNotEmpty(toStrValue)) {
- if (data.getClass().isArray()) {
- if (ArrayUtils.isNotEmpty((String[]) data)) {
- map.put(fieldName, data);
- }
- } else {
- map.put(fieldName, data);
- }
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return map;
- }
- /**
- * @Description 包含空字段
- * @Param [obj] 对象转成map集合
- * @return java.util.Map<java.lang.String, java.lang.Object>
- * @Author yangyj
- * @Date 2021.05.27 14:33
- **/
- // public static Map<String, Object> objectToMapNull(Object obj) {
- // Map<String, Object> map = new HashMap<String,Object>();
- // try {
- // Class<?> clazz = obj.getClass();
- // for (Field field : clazz.getDeclaredFields()) {
- // field.setAccessible(true);
- // if(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) {
- // continue;
- // }
- // String fieldName = field.getName();
- // Object data =field.get(obj);
- // Object toStrValue = handleNull(data);
- // if(isNotEmpty(toStrValue)&&data.getClass().isArray()){
- // if(ArrayUtils.isNotEmpty((String[])data)){
- // map.put(fieldName,data);
- // }
- // }else if(isNotEmpty(toStrValue)&&data instanceof Date){
- // map.put(fieldName, DateUtil.dateToStr((Date) data,"yyyy-MM-dd HH:mm:ss"));
- // }else {
- // map.put(fieldName,toStrValue);
- // }
- // }
- // }catch (Exception e){
- // e.printStackTrace();
- // }
- // return map;
- // }
- /**
- * @return java.lang.String
- * @Description 返回字符串后n个字符
- * @Param [str, n]
- * @Author yangyj
- * @Date 2021.08.06 16:31
- **/
- public static String tail(String str, int n) {
- if (isNotEmpty(str) && str.length() >= n) {
- return str.substring(str.length() - n);
- } else {
- return str;
- }
- }
- /**
- * @return java.lang.String
- * @Description list转字符串
- * @Param [list, split:分隔符]
- * @Author yangyj
- * @Date 2021.08.06 17:11
- **/
- public static String join(List<String> list, String split) {
- StringBuilder sb = new StringBuilder("");
- if (ListUtils.isNotEmpty(list)) {
- for (String str : list) {
- if (StringUtils.isNotEmpty(str)) {
- sb.append(str).append(split);
- }
- }
- if (sb.length() > 0 && isNotEmpty(split)) {
- sb.delete(sb.length() - split.length(), sb.length());
- }
- }
- return sb.toString();
- }
- public static String join(List<String> list) {
- return join(list, null);
- }
- public static String join(Object... args) {
- if (args != null) {
- if (args.length > 2) {
- List<String> list = Arrays.stream(args).limit(args.length - 1).map(StringUtils::handleNull).collect(Collectors.toList());
- String split = handleNull(args[args.length - 1]);
- return join(list, split);
- } else {
- return handleNull(args[0]);
- }
- } else {
- return "";
- }
- }
- /**
- * @Description 获取实体映射所有表字段名
- * @Param [clazz]
- * @return java.util.List<java.lang.String>
- * @Author yangyj
- * @Date 2021.08.18 11:05
- **/
- // public static Map<String,String> getFiledAnnotate(Class<?> clazz) {
- // LinkedHashMap<String,String> names = new LinkedHashMap<>();
- // Field[] fields = clazz.getDeclaredFields();
- // Method[] methods=clazz.getMethods();
- // for (Field field:fields) {
- // field.setAccessible(true);
- // if(field.isAnnotationPresent(Column.class)) {
- // names.put(field.getDeclaredAnnotation(Column.class).name(),isEquals(field.getType().toString(),String.class)?"''":"null");
- // }
- // }
- // for (Method m:methods) {
- // m.setAccessible(true);
- // if(m.isAnnotationPresent(Column.class)) {
- // names.put(m.getDeclaredAnnotation(Column.class).name(),isEquals(m.getReturnType().toString(),String.class)?"''":"null");
- // }
- // }
- // return names;
- // }
- /**
- * @return java.lang.String
- * @Description 只查询出指定字段内容
- * @Param [clazz, fields]
- * @Author yangyj
- * @Date 2021.08.18 11:18
- **/
- // public static String selectFieldsBuilder(Class<?>clazz,String ...fields){
- // StringBuilder sb = new StringBuilder();
- // Map<String,String> fieldsNames=getFiledAnnotate(clazz);
- // if(MapUtils.isNotEmpty(fieldsNames)){
- // if(ArrayUtils.isEmpty(fields)){
- // sb.append(" * ");
- // }else{
- // HashSet<String> keys = new HashSet<>(Arrays.asList(fields));
- // for(Map.Entry<String,String> entry:fieldsNames.entrySet()){
- // if(!keys.contains(entry.getKey())){
- // sb.append(entry.getValue()).append(" as ");
- // }
- // sb.append(entry.getKey()).append(",");
- // }
- // if(sb.lastIndexOf(",")==sb.length()-1){
- // sb.deleteCharAt(sb.length()-1);
- // }
- // }
- // }
- // return sb.toString();
- // }
- public static String toString(byte[] bytes) {
- try {
- return new String(bytes, CHARSET_NAME);
- } catch (UnsupportedEncodingException e) {
- return "";
- }
- }
- /**
- * @return java.lang.String
- * @Description 返回保留指定小数位数字字符串
- * @Param [number, scale]
- * @Author yangyj
- * @Date 2021.10.15 10:35
- **/
- public static String number2StringZero(Object number, Object scale) {
- if (isNumber(number)) {
- if (isEmpty(scale)) {
- scale = 0;
- }
- return new BigDecimal(handleNull(number)).setScale(handleObj2Integer(scale), BigDecimal.ROUND_HALF_UP).toString();
- }
- return "";
- }
- public static String number2String(Object number, Object scale) {
- if (isNumber(number)) {
- if (isEmpty(scale)) {
- scale = 0;
- }
- String val = new BigDecimal(handleNull(number)).setScale(handleObj2Integer(scale), BigDecimal.ROUND_HALF_UP).toString();
- if (val.contains(".")) {
- return new BigDecimal(val).toString().replaceAll("(0+|\\.0+)$", "");
- }
- return val;
- }
- return "";
- }
- /*
- public static void main(String[] args) {
- System.out.println(StringUtils.number2String("-2400.0000000000055",1));
- }
- */
- /**
- * @return java.lang.String
- * @Description byte数组转Md5字符串
- * @Param [bytes]
- * @Author yangyj
- * @Date 2021.12.20 17:10
- **/
- public static String bytes2Md5(byte[] bytes) {
- StringBuffer sb = new StringBuffer("");
- byte b[] = Md5Util.md5(bytes);
- int d;
- for (int i = 0; i < b.length; i++) {
- d = b[i];
- if (d < 0) {
- d = b[i] & 0xff;
- // 与上一行效果等同
- // i += 256;
- }
- if (d < 16)
- sb.append("0");
- sb.append(Integer.toHexString(d));
- }
- return sb.toString();
- }
- /**
- * 转换为字节数组
- *
- * @param str
- * @return
- */
- public static byte[] getBytes(String str) {
- if (str != null) {
- try {
- return str.getBytes(CHARSET_NAME);
- } catch (UnsupportedEncodingException e) {
- return null;
- }
- } else {
- return null;
- }
- }
- /**
- * @param name
- * @param suffix
- * @param splitStr
- * @return
- */
- public static String addSuffixInFirst(String name, String suffix, String splitStr) {
- String newName = name;
- if (isEmpty(name)) {
- return newName;
- }
- int idx = name.indexOf(splitStr);
- if (idx > 0) {
- String templateName = name.substring(0, idx);
- String templateNameNew = templateName + suffix;
- newName = templateNameNew + name.substring(idx);
- }
- return newName;
- }
- public static String replaceSuffixInFirst(String name, String suffix, String splitStr) {
- String newName = name;
- if (isEmpty(name)) {
- return newName;
- }
- int idx = name.indexOf(splitStr);
- if (idx > 0) {
- String templateName = name.substring(0, idx);
- newName = newName.replace(templateName, suffix);
- }
- return newName;
- }
- /**
- * @return boolean
- * @Description 匹配度
- * @Param [a, b]
- * @Author yangyj
- * @Date 2022.03.24 17:40
- **/
- public static boolean suitability(String a, String b) {
- if (StringUtils.isNotEmpty(a, b)) {
- char[] bA = b.toCharArray();
- if (a.length() >= bA.length) {
- for (char c : bA) {
- if (!a.contains(String.valueOf(c))) {
- return false;
- }
- }
- return true;
- }
- }
- return false;
- }
- /**
- * @return int
- * @Description 获取最大小数位,转String计算小数位格式,超长浮点数先转double,四舍五入取五位
- * @Param [number]
- * @Author yangyj
- * @Date 2021.12.23 15:08
- **/
- /**去除有效数字后面的0*/
- public static Integer getScale(Object... number) {
- int max = 0;
- if (number != null) {
- return getScale(scaleParam(number),1);
- }
- return max;
- }
- /**保留有效数字后面的0*/
- public static Integer getScaleZero(Object... number){
- int max = 0;
- if (number != null) {
- return getScale(scaleParam(number),0);
- }
- return max;
- }
- public static List<Object> scaleParam(Object ... number){
- return Arrays.stream(number).filter(StringUtils::isNotEmpty).flatMap(e-> CustomFunction.obj2ListNe(e).stream()).distinct().filter(StringUtils::isNumber).map(e->{
- /*0.3999999999999986 检测到超长小数位先转double处理,再还原回String*/
- String tg=e.toString();
- if(tg.length()-tg.indexOf(StringPool.DOT)>6||tg.contains("e")){
- return BigDecimal.valueOf(Double.parseDouble(tg)).setScale(2, RoundingMode.HALF_UP).toString();
- }else {
- return e.toString();
- }
- }).collect(Collectors.toList());
- }
- public static final String[] SCALE_REG=new String[]{"(\\d)+.(\\d)*[0-9]","(\\d)+.(\\d)*[1-9]"};
- private static Integer getScale(List<Object> number,Integer zero){
- int max=0;
- if(ListUtils.isNotEmpty(number)){
- for (Object n : number) {
- if (StringUtils.isNotEmpty(n)) {
- String[] sa = n.toString().split(StringPool.COMMA);
- for (String s : sa) {
- Matcher m = RegexUtils.matcher(SCALE_REG[zero], s);
- if (m.find()) {
- int cp = new StringBuilder(m.group()).reverse().toString().indexOf(".");
- if (cp < 5) {
- max = Math.max(cp, max);
- }
- }
- }
- }
- }
- }
- return max;
- }
- // public static void main(String[] args) {
- // List<Double> list = new ArrayList<>();
- // list.add(Double.parseDouble("0.3999999999999986"));
- // list.add(Double.parseDouble("0.1999999999999996"));
- // list.add(Double.parseDouble("-0.21"));
- // list.add(Double.parseDouble("-1.2632e1"));
- // System.out.println(getScale(list));
- // System.out.println(getScaleZero(list));
- // }
- /* public static void main(String[] args) {
- List<Object> list = Arrays.asList("16.001",27.0,5,6);
- System.out.println(getScale(list));
- System.out.println(getScaleZero(150.0));
- System.out.println(getScale(150.0));
- }*/
- /**
- * @return java.lang.String
- * @Description 公式脚本转义
- * @Param [f]
- * @Author yangyj
- * @Date 2022.10.13 21:25
- **/
- public static String escapeFormula(String f) {
- if (isNotEmpty(f)) {
- if (f.contains(">") || f.contains("<")) {
- f = f.replace("<", "<").replace(">", ">");
- }
- if (f.contains(StringPool.QUOTE)) {
- f = f.replaceAll(StringPool.QUOTE, StringPool.SINGLE_QUOTE);
- }
- }
- return f;
- }
- }
|