StringUtils.java 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. package com.mixsmart.utils;
  2. import com.bstek.ureport.console.designer.ReportUtils;
  3. import com.jfirer.baseutil.encrypt.Md5Util;
  4. import com.mixsmart.constant.IMixConstant;
  5. import com.mixsmart.exception.NullArgumentException;
  6. import org.springblade.core.tool.utils.StringPool;
  7. import java.io.UnsupportedEncodingException;
  8. import java.lang.reflect.Field;
  9. import java.lang.reflect.Modifier;
  10. import java.math.BigDecimal;
  11. import java.text.DecimalFormat;
  12. import java.text.SimpleDateFormat;
  13. import java.util.*;
  14. import java.util.regex.Matcher;
  15. import java.util.regex.Pattern;
  16. import java.util.stream.Collectors;
  17. /**
  18. * @Description:字符串处理工具类
  19. * @author:rock
  20. * @time:2020年4月21日 下午12:05:04
  21. * @version 1.0
  22. */
  23. public class StringUtils {
  24. private StringUtils() {
  25. throw new UnsupportedOperationException("StringUtils类无法实例化");
  26. }
  27. private static final String CHARSET_NAME = "UTF-8";
  28. /**
  29. * 判断是否为空
  30. * @param value 内容
  31. * @return 为空返回:true;否则返回:false
  32. */
  33. public static boolean isEmpty(String value) {
  34. return (null==value || value.trim().length()==0);
  35. }
  36. /**
  37. * @Description 批量判读为空
  38. * @Param [values]
  39. * @return boolean
  40. * @Author yangyj
  41. * @Date 2021.11.05 13:42
  42. **/
  43. public static boolean isEmpty(Object ...values) {
  44. for(Object value:values){
  45. if(!isEmpty(value)){
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. /**
  52. * 判断是否不为空
  53. * @param value 内容
  54. * @return 不为空返回:true;否则返回:false
  55. */
  56. public static boolean isNotEmpty(String value) {
  57. return !isEmpty(value);
  58. }
  59. /**
  60. * 判断参数;如果参数<code>value</code> 为空;
  61. * 则抛出参数为空异常(运行时异常)
  62. * @param value 需要判断的参数
  63. * @param msg 提示信息
  64. */
  65. public static void isAssert(Object value, String msg) {
  66. msg = isEmpty(msg)?"提供的参数为空":msg;
  67. if(null == value) {
  68. throw new NullArgumentException(msg);
  69. }
  70. if(value instanceof String) {
  71. if(isEmpty(value.toString())) {
  72. throw new NullArgumentException(msg);
  73. }
  74. } else if(value instanceof Collection) {
  75. if(CollectionUtils.isEmpty((Collection<?>)value)) {
  76. throw new NullArgumentException(msg);
  77. }
  78. } else if(value.getClass().isArray()) {
  79. if(ArrayUtils.isEmpty((Object[])value)) {
  80. throw new NullArgumentException(msg);
  81. }
  82. }
  83. }
  84. /**
  85. * 判断值是否相等
  86. * @param value1
  87. * @param value2
  88. * @return 相等返回:true;否则返回:false
  89. */
  90. public static boolean isEquals(Object value1, Object value2) {
  91. boolean is = false;
  92. if(null != value1 && null != value2) {
  93. is = value1.toString().equals(value2.toString());
  94. } else if(null == value1 && null == value2) {
  95. is = true;
  96. }
  97. return is;
  98. }
  99. /**
  100. * 判断值是否不相等
  101. * @param value1
  102. * @param value2
  103. * @return 相等返回:true;否则返回:false
  104. */
  105. public static boolean isNotEquals(Object value1, Object value2) {
  106. return !isEquals(value1, value2);
  107. }
  108. /**
  109. * 判断值是否相等(不区分大小写)
  110. * @param value1
  111. * @param value2
  112. * @return 相等返回:true;否则返回:false
  113. */
  114. public static boolean isEqualsIgnoreCase(Object value1, Object value2) {
  115. boolean is = false;
  116. if(null != value1 && null != value1) {
  117. is = value1.toString().equalsIgnoreCase(value2.toString());
  118. } else if(null == value1 && null == value1) {
  119. is = true;
  120. }
  121. return is;
  122. }
  123. /**
  124. * 判断值是否不相等(不区分大小写)
  125. * @param value1
  126. * @param value2
  127. * @return 相等返回:true;否则返回:false
  128. */
  129. public static boolean isNotEqualsIgnoreCase(Object value1, Object value2) {
  130. return !isEqualsIgnoreCase(value1, value2);
  131. }
  132. /**
  133. * null转换为“”
  134. * @param obj
  135. * @return 返回处理后的结果
  136. */
  137. public static String handleNull(Object obj) {
  138. if (null == obj) {
  139. return "";
  140. } else {
  141. return obj.toString().trim();
  142. }
  143. }
  144. /**
  145. * 当值为null转化为“null”
  146. * @param obj
  147. * @return 返回处理后的结果
  148. */
  149. public static String nullToStr(Object obj) {
  150. if (null == obj) {
  151. return "null";
  152. } else {
  153. return obj.toString().trim();
  154. }
  155. }
  156. /**
  157. * 对象转化为整型
  158. * <p>注:该方法已过时,请用{@link #handleObj2Integer(Object)} 代替</p>
  159. * @param obj
  160. * @return 返回转化结果
  161. */
  162. public static Integer handObj2Integer(Object obj) {
  163. return handleObj2Integer(obj);
  164. }
  165. /**
  166. * 对象转化为整型
  167. * @param obj
  168. * @return 返回转化结果
  169. */
  170. public static Integer handleObj2Integer(Object obj) {
  171. if (null == obj) {
  172. return 0;
  173. } else {
  174. double value = 0;
  175. try {
  176. value = Double.parseDouble(obj.toString());
  177. } catch (Exception ex) {
  178. value = 0;
  179. }
  180. return (int)value;
  181. }
  182. }
  183. /**
  184. * @Description 对象转Double
  185. * @Param [obj, scale]
  186. * @return java.lang.Double
  187. * @Author yangyj
  188. * @Date 2022.04.20 14:15
  189. **/
  190. public static Double obj2Double(Object obj,Object scale){
  191. if(StringUtils.isNumber(obj)){
  192. if(StringUtils.isNumber(scale)){
  193. obj=new BigDecimal(StringUtils.number2String(obj,scale));
  194. }
  195. BigDecimal big = new BigDecimal(obj.toString());
  196. return big.doubleValue();
  197. }
  198. return null;
  199. }
  200. public static Double obj2Double(Object obj){
  201. return obj2Double(obj,null);
  202. }
  203. /**
  204. * 数字null转换为“0”
  205. * @param obj
  206. * @return 返回转化结果
  207. */
  208. public static String handleNumNull(Object obj) {
  209. if (null == obj) {
  210. return "0";
  211. } else {
  212. return obj.toString().trim();
  213. }
  214. }
  215. /**
  216. * 判断是否为数字(包括小数)
  217. * @param value
  218. * @return 数字返回:true;否则返回:false
  219. */
  220. public static boolean isNum(Object value) {
  221. boolean is = false;
  222. if(value != null) {
  223. Pattern pattern = Pattern.compile("\\d+|\\d+\\.\\d+");
  224. Matcher matcher = pattern.matcher(value.toString());
  225. if(matcher.matches()) {
  226. is = true;
  227. } else {
  228. is = false;
  229. }
  230. }
  231. return is;
  232. }
  233. /**
  234. * 判断是否数字整数
  235. * @param value
  236. * @return 是返回:true;否则返回:false
  237. */
  238. public static boolean isInteger(String value) {
  239. boolean is = false;
  240. Pattern pattern = Pattern.compile("\\d+");
  241. if(null != value && value.length()>1) {
  242. pattern = Pattern.compile("^[1-9]\\d+");
  243. }
  244. Matcher matcher = pattern.matcher(value);
  245. if(matcher.matches()) {
  246. is = true;
  247. } else {
  248. is = false;
  249. }
  250. return is;
  251. }
  252. /**
  253. * 判断是否小数
  254. * @param value
  255. * @return 是返回:true;否则返回:false
  256. */
  257. public static boolean isDecimal(String value) {
  258. boolean is = false;
  259. Pattern pattern = Pattern.compile("\\d+\\.\\d+");
  260. Matcher matcher = pattern.matcher(value);
  261. if(matcher.matches()) {
  262. is = true;
  263. } else {
  264. is = false;
  265. }
  266. return is;
  267. }
  268. /**
  269. * 随机生成数
  270. * @param num 要生成随机数的个数
  271. * @return 返回随机生成数
  272. */
  273. public static String randomNum(int num) {
  274. Random random = new Random();
  275. String numStr = "";
  276. for (int i = 0; i < num; i++) {
  277. numStr += random.nextInt(10);
  278. }
  279. return numStr;
  280. }
  281. /**
  282. * 按日期格式生成序列号
  283. * @param dateFormaterStr 日期格式
  284. * @return 返回序列号
  285. */
  286. public static String createSerialNum(String dateFormaterStr) {
  287. String serialNum = null;
  288. if(isNotEmpty(dateFormaterStr)) {
  289. SimpleDateFormat dateFormatter = new SimpleDateFormat(dateFormaterStr);
  290. serialNum = dateFormatter.format(new Date());
  291. dateFormatter = null;
  292. }
  293. return serialNum;
  294. }
  295. /**
  296. * 生成UUID序列号
  297. * @return 返回UUID
  298. */
  299. public static String uuid() {
  300. String value = UUID.randomUUID().toString();
  301. value = value.replaceAll("-", "");
  302. return value.substring(0, 28);
  303. }
  304. public static String uuid(int max) {
  305. String value = UUID.randomUUID().toString();
  306. value = value.replaceAll("-", "");
  307. return value.substring(0, max);
  308. }
  309. /**
  310. * 获取文件后缀
  311. * @param fileName
  312. * @return 返回文件后缀
  313. */
  314. public static String getFileSuffix(String fileName) {
  315. int index = fileName.lastIndexOf(".");
  316. if(index != -1) {
  317. return fileName.substring(index + 1);
  318. }
  319. return "";
  320. }
  321. /**
  322. * 去掉文件后缀
  323. * @param fileName
  324. * @return 返回文件后缀
  325. */
  326. public static String trimFileSuffix(String fileName) {
  327. return fileName.substring(0,fileName.lastIndexOf("."));
  328. }
  329. /**
  330. * 验证手机号码
  331. * @param phoneNo
  332. * @return 验证成功返回:true;否则返回:false
  333. */
  334. public static boolean isPhoneNO(String phoneNo){
  335. Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
  336. Matcher m = p.matcher(phoneNo);
  337. return m.matches();
  338. }
  339. /**
  340. * 验证固定电话号码
  341. * @param tel
  342. * @return 验证成功返回:true;否则返回:false
  343. */
  344. public static boolean isFixedTelephone(String tel) {
  345. Pattern p = Pattern.compile("^[0][0-9]{2,3}-[2-9][0-9]{6,7}(-[0-9]{1,4})?");
  346. Matcher m = p.matcher(tel);
  347. return m.matches();
  348. }
  349. /**
  350. * 验证匿名
  351. * @param anonymous
  352. * @return 验证成功返回:true;否则返回:false
  353. */
  354. public static boolean isAnonymous(String anonymous){
  355. Pattern p = Pattern.compile("^[\\u4e00-\\u9fa5|A-Za-z]([\\w|\\u4e00-\\u9fa5]){1,7}$");
  356. Matcher m = p.matcher(anonymous);
  357. return m.matches();
  358. }
  359. /**
  360. * 验证汉字
  361. * @param value
  362. * @return 验证成功返回:true;否则返回:false
  363. */
  364. public static boolean isChinese(String value){
  365. Pattern p = Pattern.compile("^[\\u4E00-\\u9FFF]+$");
  366. Matcher m = p.matcher(value);
  367. boolean is = m.matches();
  368. return is;
  369. }
  370. /**
  371. * 验证正则表达式
  372. * @param value
  373. * @return 验证成功返回:true;否则返回:false
  374. */
  375. public static boolean isCheckRegex(String value,String regex){
  376. Pattern p = Pattern.compile(regex);
  377. Matcher m = p.matcher(value);
  378. boolean is = m.matches();
  379. return is;
  380. }
  381. /**
  382. * 验证QQ号码
  383. * @param value
  384. * @return 验证成功返回:true;否则返回:false
  385. */
  386. public static boolean isQQ(String value) {
  387. Pattern p = Pattern.compile("^[1-9]\\d{6,11}$");
  388. Matcher m = p.matcher(value);
  389. return m.matches();
  390. }
  391. /**
  392. * 验证email
  393. * @param email
  394. * @return 验证成功返回:true;否则返回:false
  395. */
  396. public static boolean isEmail(String email) {
  397. 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}$");
  398. Matcher m = p.matcher(email);
  399. return m.matches();
  400. }
  401. /**
  402. * 秒转化为:HH:mm:SS格式
  403. * @param second
  404. * @return 返回转化后的结果
  405. */
  406. public static String secondToHHMMSS(long second){
  407. long h=0,m=0,s=0,tmp=0;
  408. if(second>=3600){
  409. h= second/3600;
  410. tmp = second%36000;
  411. if(tmp>=60) {
  412. m = tmp/60;
  413. s = tmp%60;
  414. }else {
  415. s = tmp;
  416. }
  417. }else if(second>=60){
  418. m = second/60;
  419. s = second%60;
  420. }else {
  421. s = second;
  422. }
  423. return (h>9?h:"0"+h)+":"+(m>9?m:"0"+m)+":"+(s>9?s:"0"+s);
  424. }
  425. /**
  426. * 过滤特殊字符
  427. * @param params
  428. * @return 返回过滤后的结果
  429. */
  430. public static String filterSQLParams(String params) {
  431. if(!isEmpty(params)) {
  432. StringBuilder strBuilder = new StringBuilder();
  433. strBuilder.append("'|\"|update|delete|select|drop|insert|=|;|0x|\\(|\\)|\\s|\\*|\\?|\\%|\\$");
  434. strBuilder.append("|and|exec|execute|chr|mid|master|truncate|char|declare|sitename|net user|xp_cmdshell|or");
  435. strBuilder.append("|\\+|,|like'|table|from|grant|use|group_concat|column_name|information_schema.columns");
  436. strBuilder.append("|table_schema|union|where|order|by|count");
  437. strBuilder.append("|--|,|like|//|/|#");
  438. String params1 = params.toLowerCase();
  439. params1 = params1.replaceAll(strBuilder.toString(), "");
  440. if("".equals(params1)&&!"''".equals(params)){
  441. params = params1;
  442. }
  443. params = params.replaceAll("&", "&amp");
  444. params = params.replaceAll("<", "&lt");
  445. params = params.replaceAll(">", "&gt");
  446. }
  447. return params;
  448. }
  449. /**
  450. * 根据regex分隔字符串
  451. * 然后用逗号","重组
  452. * @param ids
  453. * @param regex
  454. * @return 返回处理后的结果
  455. */
  456. public static String splitIds(String ids,String regex){
  457. String newIds = "";
  458. if(isNotEmpty(ids)) {
  459. String[] idsArr = ids.split(regex);
  460. for (int i = 0; i < idsArr.length; i++) {
  461. if(i != (idsArr.length-1)) {
  462. newIds += "'"+idsArr[i]+"',";
  463. } else {
  464. newIds += "'"+idsArr[i]+"'";
  465. }
  466. }
  467. }
  468. return newIds;
  469. }
  470. /**
  471. * 计算出文件大小
  472. * @param size
  473. * @return 返回处理后的结果;<br />
  474. * 格式为:"100 KB"或”100 M“或”100 G“
  475. */
  476. public static String fileSize(long size) {
  477. DecimalFormat df = new DecimalFormat("0.0#");
  478. long KB = 1024;
  479. long MB = KB*1024;
  480. long GB = MB*1024;
  481. String valueStr = null;
  482. if(size<0) {
  483. valueStr = "0 KB";
  484. } else if(size<KB*1024) {
  485. double value = (double)size/KB;
  486. valueStr = df.format(value)+" KB";
  487. } else if(size<MB*1024) {
  488. double value = (double)size/MB;
  489. valueStr = df.format(value)+" M";
  490. } else {
  491. double value = (double)size/GB;
  492. valueStr = df.format(value)+" G";
  493. }
  494. df = null;
  495. return valueStr;
  496. }
  497. /**
  498. * 过滤目录结构(防止参数传递目录结构)
  499. * @param value
  500. * @return 返回过滤后的结果
  501. */
  502. public static String filterFilePath(String value) {
  503. if(!StringUtils.isEmpty(value)) {
  504. value.replaceAll("\\.|/|\\\\\\\\|\\\\|:|%2F|%2E|25%|20%|%5C|60%|27%|%3A|%2A","");
  505. }
  506. return value;
  507. }
  508. /**
  509. * 判断是否含有contain指定的值
  510. * @param value
  511. * @param contain
  512. * @return 包含返回:true;否则返回:false
  513. */
  514. public static boolean isContains(String value,String contain) {
  515. boolean is = false;
  516. if(!isEmpty(value) && null != contain) {
  517. is = value.contains(contain) ?true:false;
  518. }
  519. return is;
  520. }
  521. /**
  522. * list转换为数组
  523. * @param values
  524. * @return 返回转化结果
  525. */
  526. public static String[] list2Array(Collection<String> values) {
  527. String[] valueArray = null;
  528. if(null != values && values.size()>0) {
  529. valueArray = new String[values.size()];
  530. values.toArray(valueArray);
  531. }
  532. return valueArray;
  533. }
  534. /**
  535. * 验证IP地址
  536. * @param ip
  537. * @return 验证成功返回:true;否则返回:false
  538. */
  539. public static boolean checkIp(String ip) {
  540. boolean is = false;
  541. String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
  542. + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
  543. + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
  544. + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
  545. Pattern pattern = Pattern.compile(regex);
  546. Matcher matcher = pattern.matcher(ip);
  547. if(matcher.matches()) {
  548. is = true;
  549. }
  550. return is;
  551. }
  552. /**
  553. * 集合转换为字符串,字符串之间用separater提供的参数分隔
  554. * @param values
  555. * @param separater
  556. * @return 返回处理后的结果
  557. */
  558. public static <T> String collection2String(Collection<T> values,String separater) {
  559. StringBuilder strBuilder = null;
  560. if(null != values && values.size()>0) {
  561. strBuilder = new StringBuilder();
  562. if(isEmpty(separater)) {
  563. separater = "";
  564. }
  565. for (T value : values) {
  566. strBuilder.append(StringUtils.handleNull(value) + separater);
  567. }
  568. if(isNotEmpty(separater)) {
  569. strBuilder.delete(strBuilder.length()-1, strBuilder.length());
  570. }
  571. }
  572. return strBuilder != null ? strBuilder.toString():null;
  573. }
  574. /**
  575. * 替换多个空格、换行、回车、tab符等为一个空格
  576. * @param value 预处理的字符串
  577. * @return 返回处理后的字符串
  578. */
  579. public static String removeMultiSpace(String value) {
  580. if(StringUtils.isNotEmpty(value)) {
  581. value = value.replaceAll("\\s+|\n|\r|\n|\t", " ");
  582. }
  583. return value;
  584. }
  585. /**
  586. * 替换特殊字符
  587. * @param value 预处理的字符串
  588. * @return 返回处理后的结果
  589. */
  590. public static String repaceSpecialChar(String value) {
  591. if(isNotEmpty(value)) {
  592. value = value.replaceAll("\r|\n|\t", "");
  593. }
  594. return value;
  595. }
  596. public static String changeQuotation (String value) {
  597. if(isNotEmpty(value)) {
  598. value = value.replaceAll("'", "\\\\u0027");
  599. }
  600. return value;
  601. }
  602. /**
  603. * 替换斜杠
  604. * @param value
  605. * @return 返回处理后的结果
  606. */
  607. public static String repaceSlash(String value) {
  608. if(isNotEmpty(value)) {
  609. //value = value.replaceAll("\\\\", "\\\\\\\\\\\\\\\\");
  610. value = value.replaceAll("\\\\", "\\\\\\\\");
  611. value = value.replaceAll("`", "\\\\`");
  612. value = value.replaceAll("'", "\\\\`");
  613. }
  614. return value;
  615. }
  616. /**
  617. * 替换斜杠
  618. * @param value
  619. * @return 返回处理后的结果
  620. */
  621. public static String repaceSlashCopyTable(String value) {
  622. if(isNotEmpty(value)) {
  623. //value = value.replaceAll("\\\\", "\\\\\\\\\\\\\\\\");
  624. //value = value.replaceAll("\\\\", "\\\\\\\\");
  625. value = value.replaceAll("`", "\\\\`");
  626. }
  627. return value;
  628. }
  629. /**
  630. * 去空格
  631. * @param value
  632. * @return 返回处理后的结果
  633. */
  634. public static String repaceBlank(String value) {
  635. if(isNotEmpty(value)) {
  636. value = value.replaceAll("\\s", "");
  637. }
  638. return value;
  639. }
  640. /**
  641. * 字符串转换为list
  642. * @param value 要转换的字符串
  643. * @param separater 分隔符
  644. * @return 返回转换后的List
  645. */
  646. public static List<String> string2List(String value, String separater) {
  647. List<String> lists = null;
  648. if(isNotEmpty(value)) {
  649. separater = isEmpty(separater)?",":separater;
  650. String[] array = value.split(separater);
  651. lists = Arrays.asList(array);
  652. }
  653. return lists;
  654. }
  655. /**
  656. * 字符串转换为Set
  657. * @param value 要转换的字符串
  658. * @param separater 分隔符
  659. * @return 返回转换后的Set
  660. */
  661. public static Set<String> string2Set(String value, String separater) {
  662. Set<String> sets = null;
  663. if(isNotEmpty(value)) {
  664. separater = isEmpty(separater) ? "," : separater;
  665. String[] array = value.split(separater);
  666. sets = new HashSet<String>();
  667. sets.addAll(Arrays.asList(array));
  668. }
  669. return sets;
  670. }
  671. /**
  672. * 如果值为空“”转换为NULL
  673. * @param value
  674. * @return
  675. */
  676. public static String empty2Null(String value) {
  677. return isEmpty(value) ? null : value;
  678. }
  679. /**
  680. * 移除多值,只获取第一个值;多值之间通过英文逗号分隔
  681. * @param value 需要处理的值
  682. * @return 返回处理后的值
  683. */
  684. public static String removeMultiValue(String value) {
  685. if(isEmpty(value)) {
  686. return value;
  687. }
  688. int p = value.indexOf(IMixConstant.MULTI_VALUE_SPLIT);
  689. if(p >= 0) {
  690. value = value.substring(0, p);
  691. }
  692. return value;
  693. }
  694. /**
  695. * 数组转化为字符串;
  696. * 如果<code>separate</code>为空,则采用默认值;默认值为:{@link IMixConstant#MULTI_VALUE_SPLIT}
  697. * @param objs 数组
  698. * @param separate 分隔符
  699. * @return 返回数组转化成功后的字符串;失败返回:null
  700. */
  701. public static String arrayToString(Object[] objs,String separate) {
  702. StringBuilder strBuff = null;
  703. if(null == objs || objs.length == 0) {
  704. return null;
  705. }
  706. if(StringUtils.isEmpty(separate)) {
  707. separate = IMixConstant.MULTI_VALUE_SPLIT;
  708. }
  709. strBuff = new StringBuilder();
  710. for (int i=0;i<objs.length;i++) {
  711. if(i < objs.length-1 ) {
  712. strBuff.append(String.valueOf(objs[i]) + separate);
  713. } else {
  714. strBuff.append(String.valueOf(objs[i]));
  715. }
  716. }//for
  717. objs = null;
  718. return (null != strBuff)?strBuff.toString():null;
  719. }
  720. /**
  721. * 字符串转化为数组;
  722. * 如果<code>separate</code>为空,则采用默认值;默认值为:{@link IMixConstant#MULTI_VALUE_SPLIT}
  723. * @param value 原字符串
  724. * @param separate 分隔符
  725. * @return 返回字符串分割成功后的数组
  726. */
  727. public static String[] stringToArray(String value, String separate) {
  728. String[] array = null;
  729. if(isEmpty(separate)) {
  730. separate = IMixConstant.MULTI_VALUE_SPLIT;
  731. }
  732. if(isNotEmpty(value)) {
  733. array = value.split(separate);
  734. }
  735. value = null;
  736. return array;
  737. }
  738. /**
  739. * 按separate分离成数组,判断该数组里面是否包含subStr;
  740. * 如果<code>separate</code>为空,则采用默认值;默认值为:{@link IMixConstant#MULTI_VALUE_SPLIT}
  741. * @param str 字符串
  742. * @param subStr 子字符串
  743. * @param separate 分隔符
  744. * @return 包含返回:true;否则返回:false
  745. */
  746. public static boolean isArrayContains(String str,String subStr,String separate) {
  747. if(isEmpty(str)) {
  748. return isEmpty(subStr);
  749. }
  750. if(null == subStr) {
  751. return true;
  752. }
  753. boolean is = false;
  754. if(isEmpty(separate)) {
  755. separate = IMixConstant.MULTI_VALUE_SPLIT;
  756. }
  757. String[] strArray = str.split(separate);
  758. for (int i = 0; i < strArray.length; i++) {
  759. if(subStr.equals(strArray[i].trim())) {
  760. is = true;
  761. break;
  762. }
  763. }//for
  764. return is;
  765. }
  766. /**
  767. * 判断对象是否为空,或转换为字符串后的值是否为空
  768. * @param value 需要判断的值
  769. * @return 为空(null或“”)返回true;否则返回false
  770. */
  771. public static boolean isEmpty(Object value) {
  772. if(null == value) {
  773. return true;
  774. } else {
  775. return isEmpty(handleNull(value));
  776. }
  777. }
  778. /**
  779. * 判断对象是否不为空;
  780. * @param value 需要判断的值
  781. * @return 如果不为空返回true;否则返回false
  782. */
  783. public static boolean isNotEmpty(Object value) {
  784. return !isEmpty(value);
  785. }
  786. /**
  787. * @Description 批量非空校验
  788. * @Param [values]
  789. * @return boolean
  790. * @Author yangyj
  791. * @Date 2021.10.19 17:04
  792. **/
  793. public static boolean isNotEmpty(Object ...values) {
  794. for(Object value:values){
  795. if(isEmpty(value)){
  796. return false;
  797. }
  798. }
  799. return true;
  800. }
  801. /**
  802. * 判断是否为数字,包括小数、负数
  803. * @param value
  804. * @return
  805. * @author:rock
  806. * @time:2020年4月15日 上午10:46:15
  807. */
  808. public static boolean isNumber(Object value) {
  809. if(isEmpty(value)){
  810. return false;
  811. }
  812. if(value instanceof Number){
  813. return true;
  814. }
  815. String pattern = "^[+-]?\\d+(\\.\\d+)?$";
  816. Pattern r = Pattern.compile(pattern);
  817. Matcher m = r.matcher(String.valueOf(value));
  818. return m.matches();
  819. }
  820. public static boolean isDouble(Object value){
  821. return isNumber(value)&&value.toString().contains(".");
  822. }
  823. /**
  824. * @Description
  825. * @Param [obj] 对象转成map集合
  826. * @return java.util.Map<java.lang.String,java.lang.Object>
  827. * @Author yangyj
  828. * @Date 2021.05.27 14:33
  829. **/
  830. public static Map<String, Object> objectToMap(Object obj) {
  831. Map<String, Object> map = new HashMap<String,Object>();
  832. try {
  833. Class<?> clazz = obj.getClass();
  834. for (Field field : clazz.getDeclaredFields()) {
  835. field.setAccessible(true);
  836. if(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) {
  837. continue;
  838. }
  839. String fieldName = field.getName();
  840. Object data =field.get(obj);
  841. Object toStrValue = handleNull(data);
  842. if(isNotEmpty(toStrValue)){
  843. if(data.getClass().isArray()){
  844. if(ArrayUtils.isNotEmpty((String[])data)){
  845. map.put(fieldName,data);
  846. }
  847. }else {
  848. map.put(fieldName,data);
  849. }
  850. }
  851. }
  852. }catch (Exception e){
  853. e.printStackTrace();
  854. }
  855. return map;
  856. }
  857. /**
  858. * @Description 包含空字段
  859. * @Param [obj] 对象转成map集合
  860. * @return java.util.Map<java.lang.String,java.lang.Object>
  861. * @Author yangyj
  862. * @Date 2021.05.27 14:33
  863. **/
  864. // public static Map<String, Object> objectToMapNull(Object obj) {
  865. // Map<String, Object> map = new HashMap<String,Object>();
  866. // try {
  867. // Class<?> clazz = obj.getClass();
  868. // for (Field field : clazz.getDeclaredFields()) {
  869. // field.setAccessible(true);
  870. // if(Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) {
  871. // continue;
  872. // }
  873. // String fieldName = field.getName();
  874. // Object data =field.get(obj);
  875. // Object toStrValue = handleNull(data);
  876. // if(isNotEmpty(toStrValue)&&data.getClass().isArray()){
  877. // if(ArrayUtils.isNotEmpty((String[])data)){
  878. // map.put(fieldName,data);
  879. // }
  880. // }else if(isNotEmpty(toStrValue)&&data instanceof Date){
  881. // map.put(fieldName, DateUtil.dateToStr((Date) data,"yyyy-MM-dd HH:mm:ss"));
  882. // }else {
  883. // map.put(fieldName,toStrValue);
  884. // }
  885. // }
  886. // }catch (Exception e){
  887. // e.printStackTrace();
  888. // }
  889. // return map;
  890. // }
  891. /**
  892. * @Description 返回字符串后n个字符
  893. * @Param [str, n]
  894. * @return java.lang.String
  895. * @Author yangyj
  896. * @Date 2021.08.06 16:31
  897. **/
  898. public static String tail(String str,int n){
  899. if(isNotEmpty(str)&&str.length()>=n){
  900. return str.substring(str.length()-n);
  901. }else {
  902. return str;
  903. }
  904. }
  905. /**
  906. * @Description list转字符串
  907. * @Param [list, split:分隔符]
  908. * @return java.lang.String
  909. * @Author yangyj
  910. * @Date 2021.08.06 17:11
  911. **/
  912. public static String join(List<String>list,String split){
  913. StringBuilder sb = new StringBuilder("");
  914. if(ListUtils.isNotEmpty(list)){
  915. for(String str:list){
  916. if(StringUtils.isNotEmpty(str)){
  917. sb.append(str).append(split);
  918. }
  919. }
  920. if(sb.length()>0&&isNotEmpty(split)){
  921. sb.delete(sb.length()-split.length(),sb.length());
  922. }
  923. }
  924. return sb.toString();
  925. }
  926. public static String join(List<String>list){
  927. return join(list,null);
  928. }
  929. public static String join(Object ...args){
  930. if(args!=null){
  931. if(args.length>2){
  932. List<String> list =Arrays.stream(args).limit(args.length - 1).map(StringUtils::handleNull).collect(Collectors.toList());
  933. String split=handleNull(args[args.length-1]);
  934. return join(list,split);
  935. }else{
  936. return handleNull(args[0]);
  937. }
  938. }else {
  939. return "";
  940. }
  941. }
  942. /**
  943. * @Description 获取实体映射所有表字段名
  944. * @Param [clazz]
  945. * @return java.util.List<java.lang.String>
  946. * @Author yangyj
  947. * @Date 2021.08.18 11:05
  948. **/
  949. // public static Map<String,String> getFiledAnnotate(Class<?> clazz) {
  950. // LinkedHashMap<String,String> names = new LinkedHashMap<>();
  951. // Field[] fields = clazz.getDeclaredFields();
  952. // Method[] methods=clazz.getMethods();
  953. // for (Field field:fields) {
  954. // field.setAccessible(true);
  955. // if(field.isAnnotationPresent(Column.class)) {
  956. // names.put(field.getDeclaredAnnotation(Column.class).name(),isEquals(field.getType().toString(),String.class)?"''":"null");
  957. // }
  958. // }
  959. // for (Method m:methods) {
  960. // m.setAccessible(true);
  961. // if(m.isAnnotationPresent(Column.class)) {
  962. // names.put(m.getDeclaredAnnotation(Column.class).name(),isEquals(m.getReturnType().toString(),String.class)?"''":"null");
  963. // }
  964. // }
  965. // return names;
  966. // }
  967. /**
  968. * @Description 只查询出指定字段内容
  969. * @Param [clazz, fields]
  970. * @return java.lang.String
  971. * @Author yangyj
  972. * @Date 2021.08.18 11:18
  973. **/
  974. // public static String selectFieldsBuilder(Class<?>clazz,String ...fields){
  975. // StringBuilder sb = new StringBuilder();
  976. // Map<String,String> fieldsNames=getFiledAnnotate(clazz);
  977. // if(MapUtils.isNotEmpty(fieldsNames)){
  978. // if(ArrayUtils.isEmpty(fields)){
  979. // sb.append(" * ");
  980. // }else{
  981. // HashSet<String> keys = new HashSet<>(Arrays.asList(fields));
  982. // for(Map.Entry<String,String> entry:fieldsNames.entrySet()){
  983. // if(!keys.contains(entry.getKey())){
  984. // sb.append(entry.getValue()).append(" as ");
  985. // }
  986. // sb.append(entry.getKey()).append(",");
  987. // }
  988. // if(sb.lastIndexOf(",")==sb.length()-1){
  989. // sb.deleteCharAt(sb.length()-1);
  990. // }
  991. // }
  992. // }
  993. // return sb.toString();
  994. // }
  995. public static String toString(byte[] bytes) {
  996. try {
  997. return new String(bytes, CHARSET_NAME);
  998. } catch (UnsupportedEncodingException e) {
  999. return "";
  1000. }
  1001. }
  1002. /**
  1003. * @Description 返回保留指定小数位数字字符串
  1004. * @Param [number, scale]
  1005. * @return java.lang.String
  1006. * @Author yangyj
  1007. * @Date 2021.10.15 10:35
  1008. **/
  1009. public static String number2StringZero(Object number, Object scale){
  1010. if(isNumber(number)){
  1011. if(isEmpty(scale)){
  1012. scale=0;
  1013. }
  1014. return new BigDecimal(handleNull(number)).setScale(handleObj2Integer(scale),BigDecimal.ROUND_HALF_UP).toString();
  1015. }
  1016. return "";
  1017. }
  1018. public static String number2String(Object number, Object scale){
  1019. if(isNumber(number)){
  1020. if(isEmpty(scale)){
  1021. scale=0;
  1022. }
  1023. String val =new BigDecimal(handleNull(number)).setScale(handleObj2Integer(scale),BigDecimal.ROUND_HALF_UP).toString();
  1024. if(val.contains(".")){
  1025. return new BigDecimal(val).toString().replaceAll("(0+|\\.0+)$","");
  1026. }
  1027. return val;
  1028. }
  1029. return "";
  1030. }
  1031. /*
  1032. public static void main(String[] args) {
  1033. System.out.println(StringUtils.number2String("-2400.0000000000055",1));
  1034. }
  1035. */
  1036. /**
  1037. * @Description byte数组转Md5字符串
  1038. * @Param [bytes]
  1039. * @return java.lang.String
  1040. * @Author yangyj
  1041. * @Date 2021.12.20 17:10
  1042. **/
  1043. public static String bytes2Md5(byte[] bytes){
  1044. StringBuffer sb = new StringBuffer("");
  1045. byte b[] = Md5Util.md5(bytes);
  1046. int d;
  1047. for (int i = 0; i < b.length; i++) {
  1048. d = b[i];
  1049. if (d < 0) {
  1050. d = b[i] & 0xff;
  1051. // 与上一行效果等同
  1052. // i += 256;
  1053. }
  1054. if (d < 16)
  1055. sb.append("0");
  1056. sb.append(Integer.toHexString(d));
  1057. }
  1058. return sb.toString();
  1059. }
  1060. /**
  1061. * 转换为字节数组
  1062. * @param str
  1063. * @return
  1064. */
  1065. public static byte[] getBytes(String str){
  1066. if (str != null){
  1067. try {
  1068. return str.getBytes(CHARSET_NAME);
  1069. } catch (UnsupportedEncodingException e) {
  1070. return null;
  1071. }
  1072. }else{
  1073. return null;
  1074. }
  1075. }
  1076. /**
  1077. *
  1078. * @param name
  1079. * @param suffix
  1080. * @param splitStr
  1081. * @return
  1082. */
  1083. public static String addSuffixInFirst(String name ,String suffix,String splitStr) {
  1084. String newName = name;
  1085. if (isEmpty(name)) {
  1086. return newName;
  1087. }
  1088. int idx = name.indexOf(splitStr);
  1089. if (idx > 0) {
  1090. String templateName = name.substring(0,idx);
  1091. String templateNameNew = templateName + suffix;
  1092. newName = templateNameNew + name.substring(idx);
  1093. }
  1094. return newName;
  1095. }
  1096. public static String replaceSuffixInFirst(String name ,String suffix,String splitStr){
  1097. String newName = name;
  1098. if (isEmpty(name)) {
  1099. return newName;
  1100. }
  1101. int idx = name.indexOf(splitStr);
  1102. if (idx > 0) {
  1103. String templateName = name.substring(0,idx);
  1104. newName = newName.replace(templateName, suffix);
  1105. }
  1106. return newName;
  1107. }
  1108. /**
  1109. * @Description 匹配度
  1110. * @Param [a, b]
  1111. * @return boolean
  1112. * @Author yangyj
  1113. * @Date 2022.03.24 17:40
  1114. **/
  1115. public static boolean suitability(String a,String b){
  1116. if(StringUtils.isNotEmpty(a,b)){
  1117. char[] bA=b.toCharArray();
  1118. if(a.length()>=bA.length){
  1119. for (char c : bA) {
  1120. if (!a.contains(String.valueOf(c))) {
  1121. return false;
  1122. }
  1123. }
  1124. return true;
  1125. }
  1126. }
  1127. return false;
  1128. }
  1129. /**
  1130. * @Description 获取最大小数位
  1131. * @Param [number]
  1132. * @return int
  1133. * @Author yangyj
  1134. * @Date 2021.12.23 15:08
  1135. **/
  1136. public static Integer getScale(Object ...number){
  1137. int max=0;
  1138. if(number!=null) {
  1139. for (Object n : number) {
  1140. if (StringUtils.isNotEmpty(n)) {
  1141. String[] sa = n.toString().split(",");
  1142. for (String s : sa) {
  1143. Matcher m = RegexUtils.matcher("(\\d)+.(\\d)+", s);
  1144. if (m.find()) {
  1145. int cp=new StringBuilder(m.group()).reverse().toString().indexOf(".");
  1146. if(cp<5) {
  1147. max = Math.max(cp, max);
  1148. }
  1149. }
  1150. }
  1151. }
  1152. }
  1153. }
  1154. return max;
  1155. }
  1156. /**
  1157. * @Description 公式脚本转义
  1158. * @Param [f]
  1159. * @return java.lang.String
  1160. * @Author yangyj
  1161. * @Date 2022.10.13 21:25
  1162. **/
  1163. public static String escapeFormula(String f){
  1164. if(isNotEmpty(f)) {
  1165. if (f.contains("&gt;") || f.contains("&lt;")) {
  1166. f = f.replace("&lt;", "<").replace("&gt;", ">");
  1167. }
  1168. if (f.contains(StringPool.QUOTE)) {
  1169. f = f.replaceAll(StringPool.QUOTE, StringPool.SINGLE_QUOTE);
  1170. }
  1171. }
  1172. return f;
  1173. }
  1174. }