StringUtils.java 13 KB

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