12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304 |
- 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.text.DecimalFormat;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- /**
- * @Description:字符串处理工具类
- * @author:rock
- * @time:2020年4月21日 下午12:05:04
- * @version 1.0
- */
- 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);
- }
- /**
- * @Description 批量判读为空
- * @Param [values]
- * @return boolean
- * @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;
- }
- }
- /**
- * @Description 对象转Double
- * @Param [obj, scale]
- * @return java.lang.Double
- * @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);
- }
- /**
- * @Description 批量非空校验
- * @Param [values]
- * @return boolean
- * @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;
- }
-
-
- /**
- * 判断是否为数字,包括小数、负数
- * @param value
- * @return
- * @author:rock
- * @time:2020年4月15日 上午10:46:15
- */
- public static boolean isNumber(Object value) {
- if(isEmpty(value)){
- return false;
- }
- if(value instanceof Number){
- return true;
- }
- String pattern = "^[+-]?\\d+(\\.\\d+)?$";
- 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(".");
- }
- /**
- * @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> 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;
- // }
- /**
- * @Description 返回字符串后n个字符
- * @Param [str, n]
- * @return java.lang.String
- * @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;
- }
- }
- /**
- * @Description list转字符串
- * @Param [list, split:分隔符]
- * @return java.lang.String
- * @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;
- // }
- /**
- * @Description 只查询出指定字段内容
- * @Param [clazz, fields]
- * @return java.lang.String
- * @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 "";
- }
- }
-
- /**
- * @Description 返回保留指定小数位数字字符串
- * @Param [number, scale]
- * @return java.lang.String
- * @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));
- }
- */
- /**
- * @Description byte数组转Md5字符串
- * @Param [bytes]
- * @return java.lang.String
- * @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;
- }
- /**
- * @Description 匹配度
- * @Param [a, b]
- * @return boolean
- * @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;
- }
- /**
- * @Description 获取最大小数位
- * @Param [number]
- * @return int
- * @Author yangyj
- * @Date 2021.12.23 15:08
- **/
- public static Integer getScale(Object ...number){
- int max=0;
- if(number!=null) {
- for (Object n : number) {
- if (StringUtils.isNotEmpty(n)) {
- String[] sa = n.toString().split(",");
- for (String s : sa) {
- Matcher m = RegexUtils.matcher("(\\d)+.(\\d)+", s);
- if (m.find()) {
- int cp=new StringBuilder(m.group()).reverse().toString().indexOf(".");
- if(cp<5) {
- max = Math.max(cp, max);
- }
- }
- }
- }
- }
- }
- return max;
- }
- /**
- * @Description 公式脚本转义
- * @Param [f]
- * @return java.lang.String
- * @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;
- }
- }
|