StringUtil.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. package com.simuwang.base.common.util;
  2. import cn.hutool.core.util.StrUtil;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import cn.hutool.core.text.StrFormatter;
  7. import com.simuwang.base.common.conts.Constants;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.springframework.util.AntPathMatcher;
  10. import java.util.*;
  11. public class StringUtil {
  12. private static final List<String> DATE_FORMATS = Arrays.asList("yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd");
  13. /** 空字符串 */
  14. private static final String NULLSTR = "";
  15. private static final int STRING_BUILDER_SIZE = 256;
  16. public static final String SPACE = " ";
  17. public static final String EMPTY = "";
  18. public static final String LF = "\n";
  19. public static final String CR = "\r";
  20. /** 下划线 */
  21. private static final char SEPARATOR = '_';
  22. public static String retainChineseCharacters(String value) {
  23. if (StrUtil.isBlank(value)) {
  24. return null;
  25. }
  26. // 正则表达式匹配中文字符
  27. String regex = "[^\\u4e00-\\u9fa5]";
  28. // 使用空字符串替换所有非中文字符
  29. return value.replaceAll(regex, "");
  30. }
  31. public static boolean isValidDate(String dateString) {
  32. if (StrUtil.isBlank(dateString)) {
  33. return false;
  34. }
  35. dateString = dateString.replaceAll("年", "-").replaceAll("月", "-");
  36. for (String format : DATE_FORMATS) {
  37. SimpleDateFormat sdf = new SimpleDateFormat(format);
  38. // 设置严格模式
  39. sdf.setLenient(false);
  40. try {
  41. sdf.parse(dateString);
  42. return true;
  43. } catch (Exception e) {
  44. }
  45. }
  46. return false;
  47. }
  48. public static boolean isNumeric(String value) {
  49. if (StrUtil.isBlank(value)) {
  50. return false;
  51. }
  52. return value.matches("^[-+]?[0-9]*\\.?[0-9]+$");
  53. }
  54. /**
  55. * 获取参数不为空值
  56. *
  57. * @param value defaultValue 要判断的value
  58. * @return value 返回值
  59. */
  60. public static <T> T nvl(T value, T defaultValue)
  61. {
  62. return value != null ? value : defaultValue;
  63. }
  64. /**
  65. * * 判断一个Collection是否为空, 包含List,Set,Queue
  66. *
  67. * @param coll 要判断的Collection
  68. * @return true:为空 false:非空
  69. */
  70. public static boolean isEmpty(Collection<?> coll)
  71. {
  72. return isNull(coll) || coll.isEmpty();
  73. }
  74. /**
  75. * * 判断一个Collection是否非空,包含List,Set,Queue
  76. *
  77. * @param coll 要判断的Collection
  78. * @return true:非空 false:空
  79. */
  80. public static boolean isNotEmpty(Collection<?> coll)
  81. {
  82. return !isEmpty(coll);
  83. }
  84. /**
  85. * * 判断一个对象数组是否为空
  86. *
  87. * @param objects 要判断的对象数组
  88. ** @return true:为空 false:非空
  89. */
  90. public static boolean isEmpty(Object[] objects)
  91. {
  92. return isNull(objects) || (objects.length == 0);
  93. }
  94. /**
  95. * * 判断一个对象数组是否非空
  96. *
  97. * @param objects 要判断的对象数组
  98. * @return true:非空 false:空
  99. */
  100. public static boolean isNotEmpty(Object[] objects)
  101. {
  102. return !isEmpty(objects);
  103. }
  104. /**
  105. * * 判断一个Map是否为空
  106. *
  107. * @param map 要判断的Map
  108. * @return true:为空 false:非空
  109. */
  110. public static boolean isEmpty(Map<?, ?> map)
  111. {
  112. return isNull(map) || map.isEmpty();
  113. }
  114. /**
  115. * * 判断一个Map是否为空
  116. *
  117. * @param map 要判断的Map
  118. * @return true:非空 false:空
  119. */
  120. public static boolean isNotEmpty(Map<?, ?> map)
  121. {
  122. return !isEmpty(map);
  123. }
  124. /**
  125. * * 判断一个字符串是否为空串
  126. *
  127. * @param str String
  128. * @return true:为空 false:非空
  129. */
  130. public static boolean isEmpty(String str)
  131. {
  132. return isNull(str) || NULLSTR.equals(str.trim());
  133. }
  134. /**
  135. * * 判断一个字符串是否为非空串
  136. *
  137. * @param str String
  138. * @return true:非空串 false:空串
  139. */
  140. public static boolean isNotEmpty(String str)
  141. {
  142. return !isEmpty(str);
  143. }
  144. /**
  145. * * 判断一个对象是否为空
  146. *
  147. * @param object Object
  148. * @return true:为空 false:非空
  149. */
  150. public static boolean isNull(Object object)
  151. {
  152. return object == null;
  153. }
  154. /**
  155. * * 判断一个对象是否非空
  156. *
  157. * @param object Object
  158. * @return true:非空 false:空
  159. */
  160. public static boolean isNotNull(Object object)
  161. {
  162. return !isNull(object);
  163. }
  164. /**
  165. * * 判断一个对象是否是数组类型(Java基本型别的数组)
  166. *
  167. * @param object 对象
  168. * @return true:是数组 false:不是数组
  169. */
  170. public static boolean isArray(Object object)
  171. {
  172. return isNotNull(object) && object.getClass().isArray();
  173. }
  174. /**
  175. * 去空格
  176. */
  177. public static String trim(String str)
  178. {
  179. return (str == null ? "" : str.trim());
  180. }
  181. /**
  182. * 截取字符串
  183. *
  184. * @param str 字符串
  185. * @param start 开始
  186. * @return 结果
  187. */
  188. public static String substring(final String str, int start)
  189. {
  190. if (str == null)
  191. {
  192. return NULLSTR;
  193. }
  194. if (start < 0)
  195. {
  196. start = str.length() + start;
  197. }
  198. if (start < 0)
  199. {
  200. start = 0;
  201. }
  202. if (start > str.length())
  203. {
  204. return NULLSTR;
  205. }
  206. return str.substring(start);
  207. }
  208. /**
  209. * 截取字符串
  210. *
  211. * @param str 字符串
  212. * @param start 开始
  213. * @param end 结束
  214. * @return 结果
  215. */
  216. public static String substring(final String str, int start, int end)
  217. {
  218. if (str == null)
  219. {
  220. return NULLSTR;
  221. }
  222. if (end < 0)
  223. {
  224. end = str.length() + end;
  225. }
  226. if (start < 0)
  227. {
  228. start = str.length() + start;
  229. }
  230. if (end > str.length())
  231. {
  232. end = str.length();
  233. }
  234. if (start > end)
  235. {
  236. return NULLSTR;
  237. }
  238. if (start < 0)
  239. {
  240. start = 0;
  241. }
  242. if (end < 0)
  243. {
  244. end = 0;
  245. }
  246. return str.substring(start, end);
  247. }
  248. /**
  249. * 判断是否为空,并且不是空白字符
  250. *
  251. * @param str 要判断的value
  252. * @return 结果
  253. */
  254. public static boolean hasText(String str)
  255. {
  256. return (str != null && !str.isEmpty() && containsText(str));
  257. }
  258. private static boolean containsText(CharSequence str)
  259. {
  260. int strLen = str.length();
  261. for (int i = 0; i < strLen; i++)
  262. {
  263. if (!Character.isWhitespace(str.charAt(i)))
  264. {
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. /**
  271. * 格式化文本, {} 表示占位符<br>
  272. * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
  273. * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
  274. * 例:<br>
  275. * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
  276. * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
  277. * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
  278. *
  279. * @param template 文本模板,被替换的部分用 {} 表示
  280. * @param params 参数值
  281. * @return 格式化后的文本
  282. */
  283. public static String format(String template, Object... params)
  284. {
  285. if (isEmpty(params) || isEmpty(template))
  286. {
  287. return template;
  288. }
  289. return StrFormatter.format(template, params);
  290. }
  291. /**
  292. * 是否为http(s)://开头
  293. *
  294. * @param link 链接
  295. * @return 结果
  296. */
  297. public static boolean ishttp(String link)
  298. {
  299. return StringUtils.startsWithAny(link, Constants.HTTP, Constants.HTTPS);
  300. }
  301. /**
  302. * 字符串转set
  303. *
  304. * @param str 字符串
  305. * @param sep 分隔符
  306. * @return set集合
  307. */
  308. public static final Set<String> str2Set(String str, String sep)
  309. {
  310. return new HashSet<String>(str2List(str, sep, true, false));
  311. }
  312. /**
  313. * 字符串转list
  314. *
  315. * @param str 字符串
  316. * @param sep 分隔符
  317. * @param filterBlank 过滤纯空白
  318. * @param trim 去掉首尾空白
  319. * @return list集合
  320. */
  321. public static final List<String> str2List(String str, String sep, boolean filterBlank, boolean trim)
  322. {
  323. List<String> list = new ArrayList<String>();
  324. if (StringUtils.isEmpty(str))
  325. {
  326. return list;
  327. }
  328. // 过滤空白字符串
  329. if (filterBlank && StringUtils.isBlank(str))
  330. {
  331. return list;
  332. }
  333. String[] split = str.split(sep);
  334. for (String string : split)
  335. {
  336. if (filterBlank && StringUtils.isBlank(string))
  337. {
  338. continue;
  339. }
  340. if (trim)
  341. {
  342. string = string.trim();
  343. }
  344. list.add(string);
  345. }
  346. return list;
  347. }
  348. /**
  349. * 判断给定的collection列表中是否包含数组array 判断给定的数组array中是否包含给定的元素value
  350. *
  351. * @param collection 给定的集合
  352. * @param array 给定的数组
  353. * @return boolean 结果
  354. */
  355. public static boolean containsAny(Collection<String> collection, String... array)
  356. {
  357. if (isEmpty(collection) || isEmpty(array))
  358. {
  359. return false;
  360. }
  361. else
  362. {
  363. for (String str : array)
  364. {
  365. if (collection.contains(str))
  366. {
  367. return true;
  368. }
  369. }
  370. return false;
  371. }
  372. }
  373. /**
  374. * 驼峰转下划线命名
  375. */
  376. public static String toUnderScoreCase(String str)
  377. {
  378. if (str == null)
  379. {
  380. return null;
  381. }
  382. StringBuilder sb = new StringBuilder();
  383. // 前置字符是否大写
  384. boolean preCharIsUpperCase = true;
  385. // 当前字符是否大写
  386. boolean curreCharIsUpperCase = true;
  387. // 下一字符是否大写
  388. boolean nexteCharIsUpperCase = true;
  389. for (int i = 0; i < str.length(); i++)
  390. {
  391. char c = str.charAt(i);
  392. if (i > 0)
  393. {
  394. preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
  395. }
  396. else
  397. {
  398. preCharIsUpperCase = false;
  399. }
  400. curreCharIsUpperCase = Character.isUpperCase(c);
  401. if (i < (str.length() - 1))
  402. {
  403. nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
  404. }
  405. if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase)
  406. {
  407. sb.append(SEPARATOR);
  408. }
  409. else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase)
  410. {
  411. sb.append(SEPARATOR);
  412. }
  413. sb.append(Character.toLowerCase(c));
  414. }
  415. return sb.toString();
  416. }
  417. /**
  418. * 是否包含字符串
  419. *
  420. * @param str 验证字符串
  421. * @param strs 字符串组
  422. * @return 包含返回true
  423. */
  424. public static boolean inStringIgnoreCase(String str, String... strs)
  425. {
  426. if (str != null && strs != null)
  427. {
  428. for (String s : strs)
  429. {
  430. if (str.equalsIgnoreCase(trim(s)))
  431. {
  432. return true;
  433. }
  434. }
  435. }
  436. return false;
  437. }
  438. /**
  439. * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
  440. *
  441. * @param name 转换前的下划线大写方式命名的字符串
  442. * @return 转换后的驼峰式命名的字符串
  443. */
  444. public static String convertToCamelCase(String name)
  445. {
  446. StringBuilder result = new StringBuilder();
  447. // 快速检查
  448. if (name == null || name.isEmpty())
  449. {
  450. // 没必要转换
  451. return "";
  452. }
  453. else if (!name.contains("_"))
  454. {
  455. // 不含下划线,仅将首字母大写
  456. return name.substring(0, 1).toUpperCase() + name.substring(1);
  457. }
  458. // 用下划线将原始字符串分割
  459. String[] camels = name.split("_");
  460. for (String camel : camels)
  461. {
  462. // 跳过原始字符串中开头、结尾的下换线或双重下划线
  463. if (camel.isEmpty())
  464. {
  465. continue;
  466. }
  467. // 首字母大写
  468. result.append(camel.substring(0, 1).toUpperCase());
  469. result.append(camel.substring(1).toLowerCase());
  470. }
  471. return result.toString();
  472. }
  473. /**
  474. * 驼峰式命名法
  475. * 例如:user_name->userName
  476. */
  477. public static String toCamelCase(String s)
  478. {
  479. if (s == null)
  480. {
  481. return null;
  482. }
  483. if (s.indexOf(SEPARATOR) == -1)
  484. {
  485. return s;
  486. }
  487. s = s.toLowerCase();
  488. StringBuilder sb = new StringBuilder(s.length());
  489. boolean upperCase = false;
  490. for (int i = 0; i < s.length(); i++)
  491. {
  492. char c = s.charAt(i);
  493. if (c == SEPARATOR)
  494. {
  495. upperCase = true;
  496. }
  497. else if (upperCase)
  498. {
  499. sb.append(Character.toUpperCase(c));
  500. upperCase = false;
  501. }
  502. else
  503. {
  504. sb.append(c);
  505. }
  506. }
  507. return sb.toString();
  508. }
  509. /**
  510. * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
  511. *
  512. * @param str 指定字符串
  513. * @param strs 需要检查的字符串数组
  514. * @return 是否匹配
  515. */
  516. public static boolean matches(String str, List<String> strs)
  517. {
  518. if (isEmpty(str) || isEmpty(strs))
  519. {
  520. return false;
  521. }
  522. for (String pattern : strs)
  523. {
  524. if (isMatch(pattern, str))
  525. {
  526. return true;
  527. }
  528. }
  529. return false;
  530. }
  531. /**
  532. * 判断url是否与规则配置:
  533. * ? 表示单个字符;
  534. * * 表示一层路径内的任意字符串,不可跨层级;
  535. * ** 表示任意层路径;
  536. *
  537. * @param pattern 匹配规则
  538. * @param url 需要匹配的url
  539. * @return
  540. */
  541. public static boolean isMatch(String pattern, String url)
  542. {
  543. AntPathMatcher matcher = new AntPathMatcher();
  544. return matcher.match(pattern, url);
  545. }
  546. @SuppressWarnings("unchecked")
  547. public static <T> T cast(Object obj)
  548. {
  549. return (T) obj;
  550. }
  551. /**
  552. * 数字左边补齐0,使之达到指定长度。注意,如果数字转换为字符串后,长度大于size,则只保留 最后size个字符。
  553. *
  554. * @param num 数字对象
  555. * @param size 字符串指定长度
  556. * @return 返回数字的字符串格式,该字符串为指定长度。
  557. */
  558. public static final String padl(final Number num, final int size)
  559. {
  560. return padl(num.toString(), size, '0');
  561. }
  562. /**
  563. * 字符串左补齐。如果原始字符串s长度大于size,则只保留最后size个字符。
  564. *
  565. * @param s 原始字符串
  566. * @param size 字符串指定长度
  567. * @param c 用于补齐的字符
  568. * @return 返回指定长度的字符串,由原字符串左补齐或截取得到。
  569. */
  570. public static final String padl(final String s, final int size, final char c)
  571. {
  572. final StringBuilder sb = new StringBuilder(size);
  573. if (s != null)
  574. {
  575. final int len = s.length();
  576. if (s.length() <= size)
  577. {
  578. for (int i = size - len; i > 0; i--)
  579. {
  580. sb.append(c);
  581. }
  582. sb.append(s);
  583. }
  584. else
  585. {
  586. return s.substring(len - size, len);
  587. }
  588. }
  589. else
  590. {
  591. for (int i = size; i > 0; i--)
  592. {
  593. sb.append(c);
  594. }
  595. }
  596. return sb.toString();
  597. }
  598. public static boolean equals(CharSequence cs1, CharSequence cs2) {
  599. if (cs1 == cs2) {
  600. return true;
  601. } else if (cs1 != null && cs2 != null) {
  602. if (cs1.length() != cs2.length()) {
  603. return false;
  604. } else if (cs1 instanceof String && cs2 instanceof String) {
  605. return cs1.equals(cs2);
  606. } else {
  607. int length = cs1.length();
  608. for(int i = 0; i < length; ++i) {
  609. if (cs1.charAt(i) != cs2.charAt(i)) {
  610. return false;
  611. }
  612. }
  613. return true;
  614. }
  615. } else {
  616. return false;
  617. }
  618. }
  619. }