StringUtils.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. package com.lqbz.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.lqbz.common.constant.Constants;
  10. import com.lqbz.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. * 获取参数不为空值
  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 start 开始
  154. * @return 结果
  155. */
  156. public static String substring(final String str, int start)
  157. {
  158. if (str == null)
  159. {
  160. return NULLSTR;
  161. }
  162. if (start < 0)
  163. {
  164. start = str.length() + start;
  165. }
  166. if (start < 0)
  167. {
  168. start = 0;
  169. }
  170. if (start > str.length())
  171. {
  172. return NULLSTR;
  173. }
  174. return str.substring(start);
  175. }
  176. /**
  177. * 截取字符串
  178. *
  179. * @param str 字符串
  180. * @param start 开始
  181. * @param end 结束
  182. * @return 结果
  183. */
  184. public static String substring(final String str, int start, int end)
  185. {
  186. if (str == null)
  187. {
  188. return NULLSTR;
  189. }
  190. if (end < 0)
  191. {
  192. end = str.length() + end;
  193. }
  194. if (start < 0)
  195. {
  196. start = str.length() + start;
  197. }
  198. if (end > str.length())
  199. {
  200. end = str.length();
  201. }
  202. if (start > end)
  203. {
  204. return NULLSTR;
  205. }
  206. if (start < 0)
  207. {
  208. start = 0;
  209. }
  210. if (end < 0)
  211. {
  212. end = 0;
  213. }
  214. return str.substring(start, end);
  215. }
  216. /**
  217. * 判断是否为空,并且不是空白字符
  218. *
  219. * @param str 要判断的value
  220. * @return 结果
  221. */
  222. public static boolean hasText(String str)
  223. {
  224. return (str != null && !str.isEmpty() && containsText(str));
  225. }
  226. private static boolean containsText(CharSequence str)
  227. {
  228. int strLen = str.length();
  229. for (int i = 0; i < strLen; i++)
  230. {
  231. if (!Character.isWhitespace(str.charAt(i)))
  232. {
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. /**
  239. * 格式化文本, {} 表示占位符<br>
  240. * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
  241. * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
  242. * 例:<br>
  243. * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
  244. * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
  245. * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
  246. *
  247. * @param template 文本模板,被替换的部分用 {} 表示
  248. * @param params 参数值
  249. * @return 格式化后的文本
  250. */
  251. public static String format(String template, Object... params)
  252. {
  253. if (isEmpty(params) || isEmpty(template))
  254. {
  255. return template;
  256. }
  257. return StrFormatter.format(template, params);
  258. }
  259. /**
  260. * 是否为http(s)://开头
  261. *
  262. * @param link 链接
  263. * @return 结果
  264. */
  265. public static boolean ishttp(String link)
  266. {
  267. return StringUtils.startsWithAny(link, Constants.HTTP, Constants.HTTPS);
  268. }
  269. /**
  270. * 字符串转set
  271. *
  272. * @param str 字符串
  273. * @param sep 分隔符
  274. * @return set集合
  275. */
  276. public static final Set<String> str2Set(String str, String sep)
  277. {
  278. return new HashSet<String>(str2List(str, sep, true, false));
  279. }
  280. /**
  281. * 字符串转list
  282. *
  283. * @param str 字符串
  284. * @param sep 分隔符
  285. * @param filterBlank 过滤纯空白
  286. * @param trim 去掉首尾空白
  287. * @return list集合
  288. */
  289. public static final List<String> str2List(String str, String sep, boolean filterBlank, boolean trim)
  290. {
  291. List<String> list = new ArrayList<String>();
  292. if (StringUtils.isEmpty(str))
  293. {
  294. return list;
  295. }
  296. // 过滤空白字符串
  297. if (filterBlank && StringUtils.isBlank(str))
  298. {
  299. return list;
  300. }
  301. String[] split = str.split(sep);
  302. for (String string : split)
  303. {
  304. if (filterBlank && StringUtils.isBlank(string))
  305. {
  306. continue;
  307. }
  308. if (trim)
  309. {
  310. string = string.trim();
  311. }
  312. list.add(string);
  313. }
  314. return list;
  315. }
  316. /**
  317. * 判断给定的collection列表中是否包含数组array 判断给定的数组array中是否包含给定的元素value
  318. *
  319. * @param collection 给定的集合
  320. * @param array 给定的数组
  321. * @return boolean 结果
  322. */
  323. public static boolean containsAny(Collection<String> collection, String... array)
  324. {
  325. if (isEmpty(collection) || isEmpty(array))
  326. {
  327. return false;
  328. }
  329. else
  330. {
  331. for (String str : array)
  332. {
  333. if (collection.contains(str))
  334. {
  335. return true;
  336. }
  337. }
  338. return false;
  339. }
  340. }
  341. /**
  342. * 查找指定字符串是否包含指定字符串列表中的任意一个字符串同时串忽略大小写
  343. *
  344. * @param cs 指定字符串
  345. * @param searchCharSequences 需要检查的字符串数组
  346. * @return 是否包含任意一个字符串
  347. */
  348. public static boolean containsAnyIgnoreCase(CharSequence cs, CharSequence... searchCharSequences)
  349. {
  350. if (isEmpty(cs) || isEmpty(searchCharSequences))
  351. {
  352. return false;
  353. }
  354. for (CharSequence testStr : searchCharSequences)
  355. {
  356. if (containsIgnoreCase(cs, testStr))
  357. {
  358. return true;
  359. }
  360. }
  361. return false;
  362. }
  363. /**
  364. * 驼峰转下划线命名
  365. */
  366. public static String toUnderScoreCase(String str)
  367. {
  368. if (str == null)
  369. {
  370. return null;
  371. }
  372. StringBuilder sb = new StringBuilder();
  373. // 前置字符是否大写
  374. boolean preCharIsUpperCase = true;
  375. // 当前字符是否大写
  376. boolean curreCharIsUpperCase = true;
  377. // 下一字符是否大写
  378. boolean nexteCharIsUpperCase = true;
  379. for (int i = 0; i < str.length(); i++)
  380. {
  381. char c = str.charAt(i);
  382. if (i > 0)
  383. {
  384. preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
  385. }
  386. else
  387. {
  388. preCharIsUpperCase = false;
  389. }
  390. curreCharIsUpperCase = Character.isUpperCase(c);
  391. if (i < (str.length() - 1))
  392. {
  393. nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
  394. }
  395. if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase)
  396. {
  397. sb.append(SEPARATOR);
  398. }
  399. else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase)
  400. {
  401. sb.append(SEPARATOR);
  402. }
  403. sb.append(Character.toLowerCase(c));
  404. }
  405. return sb.toString();
  406. }
  407. /**
  408. * 是否包含字符串
  409. *
  410. * @param str 验证字符串
  411. * @param strs 字符串组
  412. * @return 包含返回true
  413. */
  414. public static boolean inStringIgnoreCase(String str, String... strs)
  415. {
  416. if (str != null && strs != null)
  417. {
  418. for (String s : strs)
  419. {
  420. if (str.equalsIgnoreCase(trim(s)))
  421. {
  422. return true;
  423. }
  424. }
  425. }
  426. return false;
  427. }
  428. /**
  429. * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
  430. *
  431. * @param name 转换前的下划线大写方式命名的字符串
  432. * @return 转换后的驼峰式命名的字符串
  433. */
  434. public static String convertToCamelCase(String name)
  435. {
  436. StringBuilder result = new StringBuilder();
  437. // 快速检查
  438. if (name == null || name.isEmpty())
  439. {
  440. // 没必要转换
  441. return "";
  442. }
  443. else if (!name.contains("_"))
  444. {
  445. // 不含下划线,仅将首字母大写
  446. return name.substring(0, 1).toUpperCase() + name.substring(1);
  447. }
  448. // 用下划线将原始字符串分割
  449. String[] camels = name.split("_");
  450. for (String camel : camels)
  451. {
  452. // 跳过原始字符串中开头、结尾的下换线或双重下划线
  453. if (camel.isEmpty())
  454. {
  455. continue;
  456. }
  457. // 首字母大写
  458. result.append(camel.substring(0, 1).toUpperCase());
  459. result.append(camel.substring(1).toLowerCase());
  460. }
  461. return result.toString();
  462. }
  463. /**
  464. * 驼峰式命名法
  465. * 例如:user_name->userName
  466. */
  467. public static String toCamelCase(String s)
  468. {
  469. if (s == null)
  470. {
  471. return null;
  472. }
  473. if (s.indexOf(SEPARATOR) == -1)
  474. {
  475. return s;
  476. }
  477. s = s.toLowerCase();
  478. StringBuilder sb = new StringBuilder(s.length());
  479. boolean upperCase = false;
  480. for (int i = 0; i < s.length(); i++)
  481. {
  482. char c = s.charAt(i);
  483. if (c == SEPARATOR)
  484. {
  485. upperCase = true;
  486. }
  487. else if (upperCase)
  488. {
  489. sb.append(Character.toUpperCase(c));
  490. upperCase = false;
  491. }
  492. else
  493. {
  494. sb.append(c);
  495. }
  496. }
  497. return sb.toString();
  498. }
  499. /**
  500. * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
  501. *
  502. * @param str 指定字符串
  503. * @param strs 需要检查的字符串数组
  504. * @return 是否匹配
  505. */
  506. public static boolean matches(String str, List<String> strs)
  507. {
  508. if (isEmpty(str) || isEmpty(strs))
  509. {
  510. return false;
  511. }
  512. for (String pattern : strs)
  513. {
  514. if (isMatch(pattern, str))
  515. {
  516. return true;
  517. }
  518. }
  519. return false;
  520. }
  521. /**
  522. * 判断url是否与规则配置:
  523. * ? 表示单个字符;
  524. * * 表示一层路径内的任意字符串,不可跨层级;
  525. * ** 表示任意层路径;
  526. *
  527. * @param pattern 匹配规则
  528. * @param url 需要匹配的url
  529. * @return
  530. */
  531. public static boolean isMatch(String pattern, String url)
  532. {
  533. AntPathMatcher matcher = new AntPathMatcher();
  534. return matcher.match(pattern, url);
  535. }
  536. @SuppressWarnings("unchecked")
  537. public static <T> T cast(Object obj)
  538. {
  539. return (T) obj;
  540. }
  541. /**
  542. * 数字左边补齐0,使之达到指定长度。注意,如果数字转换为字符串后,长度大于size,则只保留 最后size个字符。
  543. *
  544. * @param num 数字对象
  545. * @param size 字符串指定长度
  546. * @return 返回数字的字符串格式,该字符串为指定长度。
  547. */
  548. public static final String padl(final Number num, final int size)
  549. {
  550. return padl(num.toString(), size, '0');
  551. }
  552. /**
  553. * 字符串左补齐。如果原始字符串s长度大于size,则只保留最后size个字符。
  554. *
  555. * @param s 原始字符串
  556. * @param size 字符串指定长度
  557. * @param c 用于补齐的字符
  558. * @return 返回指定长度的字符串,由原字符串左补齐或截取得到。
  559. */
  560. public static final String padl(final String s, final int size, final char c)
  561. {
  562. final StringBuilder sb = new StringBuilder(size);
  563. if (s != null)
  564. {
  565. final int len = s.length();
  566. if (s.length() <= size)
  567. {
  568. for (int i = size - len; i > 0; i--)
  569. {
  570. sb.append(c);
  571. }
  572. sb.append(s);
  573. }
  574. else
  575. {
  576. return s.substring(len - size, len);
  577. }
  578. }
  579. else
  580. {
  581. for (int i = size; i > 0; i--)
  582. {
  583. sb.append(c);
  584. }
  585. }
  586. return sb.toString();
  587. }
  588. }