StringUtils.java 22 KB

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