StringUtils.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package com.ruoyi.common.utils;
  2. import java.util.Collection;
  3. import java.util.Map;
  4. import com.ruoyi.common.support.StrFormatter;
  5. /**
  6. * 字符串工具类
  7. *
  8. * @author ruoyi
  9. */
  10. public class StringUtils extends org.apache.commons.lang3.StringUtils
  11. {
  12. /** 空字符串 */
  13. private static final String NULLSTR = "";
  14. /** 下划线 */
  15. private static final char SEPARATOR = '_';
  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 start 开始
  148. * @return 结果
  149. */
  150. public static String substring(final String str, int start)
  151. {
  152. if (str == null)
  153. {
  154. return NULLSTR;
  155. }
  156. if (start < 0)
  157. {
  158. start = str.length() + start;
  159. }
  160. if (start < 0)
  161. {
  162. start = 0;
  163. }
  164. if (start > str.length())
  165. {
  166. return NULLSTR;
  167. }
  168. return str.substring(start);
  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. {
  180. if (str == null)
  181. {
  182. return NULLSTR;
  183. }
  184. if (end < 0)
  185. {
  186. end = str.length() + end;
  187. }
  188. if (start < 0)
  189. {
  190. start = str.length() + start;
  191. }
  192. if (end > str.length())
  193. {
  194. end = str.length();
  195. }
  196. if (start > end)
  197. {
  198. return NULLSTR;
  199. }
  200. if (start < 0)
  201. {
  202. start = 0;
  203. }
  204. if (end < 0)
  205. {
  206. end = 0;
  207. }
  208. return str.substring(start, end);
  209. }
  210. /**
  211. * 格式化文本, {} 表示占位符<br>
  212. * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
  213. * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
  214. * 例:<br>
  215. * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
  216. * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
  217. * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
  218. *
  219. * @param template 文本模板,被替换的部分用 {} 表示
  220. * @param params 参数值
  221. * @return 格式化后的文本
  222. */
  223. public static String format(String template, Object... params)
  224. {
  225. if (isEmpty(params) || isEmpty(template))
  226. {
  227. return template;
  228. }
  229. return StrFormatter.format(template, params);
  230. }
  231. /**
  232. * 下划线转驼峰命名
  233. */
  234. public static String toUnderScoreCase(String str) {
  235. if (str == null) {
  236. return null;
  237. }
  238. StringBuilder sb = new StringBuilder();
  239. //前置字符是否大写
  240. boolean preCharIsUpperCase = true;
  241. //当前字符是否大写
  242. boolean curreCharIsUpperCase = true;
  243. //下一字符是否大写
  244. boolean nexteCharIsUpperCase = true;
  245. for (int i = 0; i < str.length(); i++) {
  246. char c = str.charAt(i);
  247. if (i > 0) {
  248. preCharIsUpperCase = Character.isUpperCase(str.charAt(i-1));;
  249. } else {
  250. preCharIsUpperCase = false;
  251. }
  252. curreCharIsUpperCase = Character.isUpperCase(c);
  253. if (i < (str.length() - 1)) {
  254. nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
  255. }
  256. if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
  257. sb.append(SEPARATOR);
  258. } else if ((i !=0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
  259. sb.append(SEPARATOR);
  260. }
  261. sb.append(Character.toLowerCase(c));
  262. }
  263. return sb.toString();
  264. }
  265. /**
  266. * 是否包含字符串
  267. *
  268. * @param str 验证字符串
  269. * @param strs 字符串组
  270. * @return 包含返回true
  271. */
  272. public static boolean inStringIgnoreCase(String str, String... strs)
  273. {
  274. if (str != null && strs != null)
  275. {
  276. for (String s : strs)
  277. {
  278. if (str.equalsIgnoreCase(trim(s)))
  279. {
  280. return true;
  281. }
  282. }
  283. }
  284. return false;
  285. }
  286. /**
  287. * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
  288. *
  289. * @param name 转换前的下划线大写方式命名的字符串
  290. * @return 转换后的驼峰式命名的字符串
  291. */
  292. public static String convertToCamelCase(String name)
  293. {
  294. StringBuilder result = new StringBuilder();
  295. // 快速检查
  296. if (name == null || name.isEmpty())
  297. {
  298. // 没必要转换
  299. return "";
  300. }
  301. else if (!name.contains("_"))
  302. {
  303. // 不含下划线,仅将首字母大写
  304. return name.substring(0, 1).toUpperCase() + name.substring(1);
  305. }
  306. // 用下划线将原始字符串分割
  307. String[] camels = name.split("_");
  308. for (String camel : camels)
  309. {
  310. // 跳过原始字符串中开头、结尾的下换线或双重下划线
  311. if (camel.isEmpty())
  312. {
  313. continue;
  314. }
  315. // 首字母大写
  316. result.append(camel.substring(0, 1).toUpperCase());
  317. result.append(camel.substring(1).toLowerCase());
  318. }
  319. return result.toString();
  320. }
  321. }