StringUtils.java 16 KB

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