StringUtils.java 13 KB

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