StringUtils.java 19 KB

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