StringUtils.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package com.ruoyi.common.utils;
  2. import java.util.Collection;
  3. import java.util.Map;
  4. import org.apache.commons.lang.text.StrBuilder;
  5. import com.ruoyi.common.support.StrFormatter;
  6. /**
  7. * 字符串工具类
  8. *
  9. * @author ruoyi
  10. */
  11. public class StringUtils
  12. {
  13. /** 空字符串 */
  14. private static final String NULLSTR = "";
  15. /** 下划线 */
  16. private static final char SEPARATOR = '_';
  17. /**
  18. * 获取参数不为空值
  19. *
  20. * @param value defaultValue 要判断的value
  21. * @return value 返回值
  22. */
  23. public static <T> T nvl(T value, T defaultValue)
  24. {
  25. return value != null ? value : defaultValue;
  26. }
  27. /**
  28. * * 判断一个Collection是否为空, 包含List,Set,Queue
  29. *
  30. * @param coll 要判断的Collection
  31. * @return true:为空 false:非空
  32. */
  33. public static boolean isEmpty(Collection<?> coll)
  34. {
  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. {
  45. return !isEmpty(coll);
  46. }
  47. /**
  48. * * 判断一个对象数组是否为空
  49. *
  50. * @param objects 要判断的对象数组
  51. ** @return true:为空 false:非空
  52. */
  53. public static boolean isEmpty(Object[] objects)
  54. {
  55. return isNull(objects) || (objects.length == 0);
  56. }
  57. /**
  58. * * 判断一个对象数组是否非空
  59. *
  60. * @param objects 要判断的对象数组
  61. * @return true:非空 false:空
  62. */
  63. public static boolean isNotEmpty(Object[] objects)
  64. {
  65. return !isEmpty(objects);
  66. }
  67. /**
  68. * * 判断一个Map是否为空
  69. *
  70. * @param map 要判断的Map
  71. * @return true:为空 false:非空
  72. */
  73. public static boolean isEmpty(Map<?, ?> map)
  74. {
  75. return isNull(map) || map.isEmpty();
  76. }
  77. /**
  78. * * 判断一个Map是否为空
  79. *
  80. * @param map 要判断的Map
  81. * @return true:非空 false:空
  82. */
  83. public static boolean isNotEmpty(Map<?, ?> map)
  84. {
  85. return !isEmpty(map);
  86. }
  87. /**
  88. * * 判断一个字符串是否为空串
  89. *
  90. * @param str String
  91. * @return true:为空 false:非空
  92. */
  93. public static boolean isEmpty(String str)
  94. {
  95. return isNull(str) || NULLSTR.equals(str.trim());
  96. }
  97. /**
  98. * * 判断一个字符串是否为非空串
  99. *
  100. * @param str String
  101. * @return true:非空串 false:空串
  102. */
  103. public static boolean isNotEmpty(String str)
  104. {
  105. return !isEmpty(str);
  106. }
  107. /**
  108. * * 判断一个对象是否为空
  109. *
  110. * @param object Object
  111. * @return true:为空 false:非空
  112. */
  113. public static boolean isNull(Object object)
  114. {
  115. return object == null;
  116. }
  117. /**
  118. * * 判断一个对象是否非空
  119. *
  120. * @param object Object
  121. * @return true:非空 false:空
  122. */
  123. public static boolean isNotNull(Object object)
  124. {
  125. return !isNull(object);
  126. }
  127. /**
  128. * * 判断一个对象是否是数组类型(Java基本型别的数组)
  129. *
  130. * @param object 对象
  131. * @return true:是数组 false:不是数组
  132. */
  133. public static boolean isArray(Object object)
  134. {
  135. return isNotNull(object) && object.getClass().isArray();
  136. }
  137. /**
  138. * 去空格
  139. */
  140. public static String trim(String str)
  141. {
  142. return (str == null ? "" : str.trim());
  143. }
  144. /**
  145. * 截取字符串
  146. *
  147. * @param str 字符串
  148. * @param start 开始
  149. * @return 结果
  150. */
  151. public static String substring(final String str, int start)
  152. {
  153. if (str == null)
  154. {
  155. return NULLSTR;
  156. }
  157. if (start < 0)
  158. {
  159. start = str.length() + start;
  160. }
  161. if (start < 0)
  162. {
  163. start = 0;
  164. }
  165. if (start > str.length())
  166. {
  167. return NULLSTR;
  168. }
  169. return str.substring(start);
  170. }
  171. /**
  172. * 截取字符串
  173. *
  174. * @param str 字符串
  175. * @param start 开始
  176. * @param end 结束
  177. * @return 结果
  178. */
  179. public static String substring(final String str, int start, int end)
  180. {
  181. if (str == null)
  182. {
  183. return NULLSTR;
  184. }
  185. if (end < 0)
  186. {
  187. end = str.length() + end;
  188. }
  189. if (start < 0)
  190. {
  191. start = str.length() + start;
  192. }
  193. if (end > str.length())
  194. {
  195. end = str.length();
  196. }
  197. if (start > end)
  198. {
  199. return NULLSTR;
  200. }
  201. if (start < 0)
  202. {
  203. start = 0;
  204. }
  205. if (end < 0)
  206. {
  207. end = 0;
  208. }
  209. return str.substring(start, end);
  210. }
  211. /**
  212. * 格式化文本, {} 表示占位符<br>
  213. * 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
  214. * 如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
  215. * 例:<br>
  216. * 通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
  217. * 转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
  218. * 转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
  219. *
  220. * @param template 文本模板,被替换的部分用 {} 表示
  221. * @param params 参数值
  222. * @return 格式化后的文本
  223. */
  224. public static String format(String template, Object... params)
  225. {
  226. if (isEmpty(params) || isEmpty(template))
  227. {
  228. return template;
  229. }
  230. return StrFormatter.format(template, params);
  231. }
  232. /**
  233. * 驼峰首字符小写
  234. */
  235. public static String uncapitalize(String str)
  236. {
  237. int strLen;
  238. if (str == null || (strLen = str.length()) == 0)
  239. {
  240. return str;
  241. }
  242. return new StrBuilder(strLen).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
  243. }
  244. /**
  245. * 下划线转驼峰命名
  246. */
  247. public static String toUnderScoreCase(String s)
  248. {
  249. if (s == null)
  250. {
  251. return null;
  252. }
  253. StringBuilder sb = new StringBuilder();
  254. boolean upperCase = false;
  255. for (int i = 0; i < s.length(); i++)
  256. {
  257. char c = s.charAt(i);
  258. boolean nextUpperCase = true;
  259. if (i < (s.length() - 1))
  260. {
  261. nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
  262. }
  263. if ((i > 0) && Character.isUpperCase(c))
  264. {
  265. if (!upperCase || !nextUpperCase)
  266. {
  267. sb.append(SEPARATOR);
  268. }
  269. upperCase = true;
  270. }
  271. else
  272. {
  273. upperCase = false;
  274. }
  275. sb.append(Character.toLowerCase(c));
  276. }
  277. return sb.toString();
  278. }
  279. /**
  280. * 是否包含字符串
  281. *
  282. * @param str 验证字符串
  283. * @param strs 字符串组
  284. * @return 包含返回true
  285. */
  286. public static boolean inStringIgnoreCase(String str, String... strs)
  287. {
  288. if (str != null && strs != null)
  289. {
  290. for (String s : strs)
  291. {
  292. if (str.equalsIgnoreCase(trim(s)))
  293. {
  294. return true;
  295. }
  296. }
  297. }
  298. return false;
  299. }
  300. /**
  301. * 将下划线大写方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。 例如:HELLO_WORLD->HelloWorld
  302. *
  303. * @param name 转换前的下划线大写方式命名的字符串
  304. * @return 转换后的驼峰式命名的字符串
  305. */
  306. public static String convertToCamelCase(String name)
  307. {
  308. StringBuilder result = new StringBuilder();
  309. // 快速检查
  310. if (name == null || name.isEmpty())
  311. {
  312. // 没必要转换
  313. return "";
  314. }
  315. else if (!name.contains("_"))
  316. {
  317. // 不含下划线,仅将首字母大写
  318. return name.substring(0, 1).toUpperCase() + name.substring(1);
  319. }
  320. // 用下划线将原始字符串分割
  321. String[] camels = name.split("_");
  322. for (String camel : camels)
  323. {
  324. // 跳过原始字符串中开头、结尾的下换线或双重下划线
  325. if (camel.isEmpty())
  326. {
  327. continue;
  328. }
  329. // 首字母大写
  330. result.append(camel.substring(0, 1).toUpperCase());
  331. result.append(camel.substring(1).toLowerCase());
  332. }
  333. return result.toString();
  334. }
  335. }