StringUtils.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. package com.ruoyi.common.utils;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.lang.Validator;
  4. import cn.hutool.core.map.MapUtil;
  5. import cn.hutool.core.util.ArrayUtil;
  6. import cn.hutool.core.util.ObjectUtil;
  7. import cn.hutool.core.util.ReUtil;
  8. import cn.hutool.core.util.StrUtil;
  9. import lombok.AccessLevel;
  10. import lombok.NoArgsConstructor;
  11. import java.util.*;
  12. /**
  13. * 字符串工具类
  14. *
  15. * @author Lion Li
  16. */
  17. @NoArgsConstructor(access = AccessLevel.PRIVATE)
  18. public class StringUtils extends org.apache.commons.lang3.StringUtils {
  19. /**
  20. * 获取参数不为空值
  21. *
  22. * @param value defaultValue 要判断的value
  23. * @return value 返回值
  24. */
  25. public static <T> T nvl(T value, T defaultValue) {
  26. return ObjectUtil.defaultIfNull(value, defaultValue);
  27. }
  28. /**
  29. * 获取参数不为空值
  30. *
  31. * @param str defaultValue 要判断的value
  32. * @return value 返回值
  33. */
  34. public static String blankToDefault(String str, String defaultValue) {
  35. return StrUtil.blankToDefault(str, defaultValue);
  36. }
  37. /**
  38. * * 判断一个Collection是否为空, 包含List,Set,Queue
  39. *
  40. * @param coll 要判断的Collection
  41. * @return true:为空 false:非空
  42. */
  43. public static boolean isEmpty(Collection<?> coll) {
  44. return CollUtil.isEmpty(coll);
  45. }
  46. /**
  47. * * 判断一个Collection是否非空,包含List,Set,Queue
  48. *
  49. * @param coll 要判断的Collection
  50. * @return true:非空 false:空
  51. */
  52. public static boolean isNotEmpty(Collection<?> coll) {
  53. return !isEmpty(coll);
  54. }
  55. /**
  56. * * 判断一个对象数组是否为空
  57. *
  58. * @param objects 要判断的对象数组
  59. * * @return true:为空 false:非空
  60. */
  61. public static boolean isEmpty(Object[] objects) {
  62. return ArrayUtil.isEmpty(objects);
  63. }
  64. /**
  65. * * 判断一个对象数组是否非空
  66. *
  67. * @param objects 要判断的对象数组
  68. * @return true:非空 false:空
  69. */
  70. public static boolean isNotEmpty(Object[] objects) {
  71. return !isEmpty(objects);
  72. }
  73. /**
  74. * * 判断一个对象是否为空
  75. *
  76. * @param object 要判断的对象数组
  77. * * @return true:为空 false:非空
  78. */
  79. public static boolean isEmpty(Object object) {
  80. return ObjectUtil.isEmpty(object);
  81. }
  82. /**
  83. * * 判断一个对象是否非空
  84. *
  85. * @param object 要判断的对象数组
  86. * @return true:非空 false:空
  87. */
  88. public static boolean isNotEmpty(Object object) {
  89. return !isEmpty(object);
  90. }
  91. /**
  92. * * 判断一个Map是否为空
  93. *
  94. * @param map 要判断的Map
  95. * @return true:为空 false:非空
  96. */
  97. public static boolean isEmpty(Map<?, ?> map) {
  98. return MapUtil.isEmpty(map);
  99. }
  100. /**
  101. * * 判断一个Map是否为空
  102. *
  103. * @param map 要判断的Map
  104. * @return true:非空 false:空
  105. */
  106. public static boolean isNotEmpty(Map<?, ?> map) {
  107. return !isEmpty(map);
  108. }
  109. /**
  110. * * 判断一个字符串是否为空串
  111. *
  112. * @param str String
  113. * @return true:为空 false:非空
  114. */
  115. public static boolean isEmpty(String str) {
  116. return StrUtil.isEmpty(str);
  117. }
  118. /**
  119. * * 判断一个字符串是否为非空串
  120. *
  121. * @param str String
  122. * @return true:非空串 false:空串
  123. */
  124. public static boolean isNotEmpty(String str) {
  125. return !isEmpty(str);
  126. }
  127. /**
  128. * * 判断一个对象是否为空
  129. *
  130. * @param object Object
  131. * @return true:为空 false:非空
  132. */
  133. public static boolean isNull(Object object) {
  134. return ObjectUtil.isNull(object);
  135. }
  136. /**
  137. * * 判断一个对象是否非空
  138. *
  139. * @param object Object
  140. * @return true:非空 false:空
  141. */
  142. public static boolean isNotNull(Object object) {
  143. return !isNull(object);
  144. }
  145. /**
  146. * * 判断一个对象是否是数组类型(Java基本型别的数组)
  147. *
  148. * @param object 对象
  149. * @return true:是数组 false:不是数组
  150. */
  151. public static boolean isArray(Object object) {
  152. return ArrayUtil.isArray(object);
  153. }
  154. /**
  155. * 去空格
  156. */
  157. public static String trim(String str) {
  158. return StrUtil.trim(str);
  159. }
  160. /**
  161. * 截取字符串
  162. *
  163. * @param str 字符串
  164. * @param start 开始
  165. * @return 结果
  166. */
  167. public static String substring(final String str, int start) {
  168. return substring(str, start, str.length());
  169. }
  170. /**
  171. * 截取字符串
  172. *
  173. * @param str 字符串
  174. * @param start 开始
  175. * @param end 结束
  176. * @return 结果
  177. */
  178. public static String substring(final String str, int start, int end) {
  179. return StrUtil.sub(str, start, end);
  180. }
  181. /**
  182. * 格式化文本, {} 表示占位符<br>
  183. * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
  184. * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
  185. * 例:<br>
  186. * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
  187. * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
  188. * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
  189. *
  190. * @param template 文本模板,被替换的部分用 {} 表示
  191. * @param params 参数值
  192. * @return 格式化后的文本
  193. */
  194. public static String format(String template, Object... params) {
  195. return StrUtil.format(template, params);
  196. }
  197. /**
  198. * 是否为http(s)://开头
  199. *
  200. * @param link 链接
  201. * @return 结果
  202. */
  203. public static boolean ishttp(String link) {
  204. return Validator.isUrl(link);
  205. }
  206. /**
  207. * 字符串转set
  208. *
  209. * @param str 字符串
  210. * @param sep 分隔符
  211. * @return set集合
  212. */
  213. public static Set<String> str2Set(String str, String sep) {
  214. return new HashSet<>(str2List(str, sep, true, false));
  215. }
  216. /**
  217. * 字符串转list
  218. *
  219. * @param str 字符串
  220. * @param sep 分隔符
  221. * @param filterBlank 过滤纯空白
  222. * @param trim 去掉首尾空白
  223. * @return list集合
  224. */
  225. public static List<String> str2List(String str, String sep, boolean filterBlank, boolean trim) {
  226. List<String> list = new ArrayList<>();
  227. if (isEmpty(str)) {
  228. return list;
  229. }
  230. // 过滤空白字符串
  231. if (filterBlank && isBlank(str)) {
  232. return list;
  233. }
  234. String[] split = str.split(sep);
  235. for (String string : split) {
  236. if (filterBlank && isBlank(string)) {
  237. continue;
  238. }
  239. if (trim) {
  240. string = trim(string);
  241. }
  242. list.add(string);
  243. }
  244. return list;
  245. }
  246. /**
  247. * 查找指定字符串是否包含指定字符串列表中的任意一个字符串同时串忽略大小写
  248. *
  249. * @param cs 指定字符串
  250. * @param searchCharSequences 需要检查的字符串数组
  251. * @return 是否包含任意一个字符串
  252. */
  253. public static boolean containsAnyIgnoreCase(CharSequence cs, CharSequence... searchCharSequences) {
  254. return StrUtil.containsAnyIgnoreCase(cs, searchCharSequences);
  255. }
  256. /**
  257. * 驼峰转下划线命名
  258. */
  259. public static String toUnderScoreCase(String str) {
  260. return StrUtil.toUnderlineCase(str);
  261. }
  262. /**
  263. * 是否包含字符串
  264. *
  265. * @param str 验证字符串
  266. * @param strs 字符串组
  267. * @return 包含返回true
  268. */
  269. public static boolean inStringIgnoreCase(String str, String... strs) {
  270. return StrUtil.equalsAnyIgnoreCase(str, strs);
  271. }
  272. /**
  273. * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
  274. *
  275. * @param name 转换前的下划线大写方式命名的字符串
  276. * @return 转换后的驼峰式命名的字符串
  277. */
  278. public static String convertToCamelCase(String name) {
  279. return StrUtil.upperFirst(StrUtil.toCamelCase(name));
  280. }
  281. /**
  282. * 驼峰式命名法 例如:user_name->userName
  283. */
  284. public static String toCamelCase(String s) {
  285. return StrUtil.toCamelCase(s);
  286. }
  287. /**
  288. * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
  289. *
  290. * @param str 指定字符串
  291. * @param strs 需要检查的字符串数组
  292. * @return 是否匹配
  293. */
  294. public static boolean matches(String str, List<String> strs) {
  295. if (isEmpty(str) || isEmpty(strs)) {
  296. return false;
  297. }
  298. for (String pattern : strs) {
  299. if (isMatch(pattern, str)) {
  300. return true;
  301. }
  302. }
  303. return false;
  304. }
  305. /**
  306. * 判断url是否与规则配置:
  307. * ? 表示单个字符;
  308. * * 表示一层路径内的任意字符串,不可跨层级;
  309. * ** 表示任意层路径;
  310. *
  311. * @param pattern 匹配规则
  312. * @param url 需要匹配的url
  313. * @return
  314. */
  315. public static boolean isMatch(String pattern, String url) {
  316. return ReUtil.isMatch(pattern, url);
  317. }
  318. @SuppressWarnings("unchecked")
  319. public static <T> T cast(Object obj) {
  320. return (T) obj;
  321. }
  322. }