StringUtils.java 16 KB

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