001a86f6b1108b9402955dc553591f6ceecb659c.svn-base 12 KB

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