StringUtil.java 16 KB

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