StringUtils.java 39 KB

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