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

ruoyi StringUtils的更多相关文章

  1. ruoyi ShiroUtils

    package com.ruoyi.framework.util; import org.apache.shiro.SecurityUtils; import org.apache.shiro.cry ...

  2. ruoyi IpUtils

    package com.ruoyi.common.utils; import java.net.InetAddress; import java.net.UnknownHostException; i ...

  3. ruoyi接口权限校验

    此文章属于ruoyi项目实战系列 ruoyi系统在前端主要通过权限字符包含与否来动态显示目录和按钮.为了防止通过http请求绕过权限限制,后端接口也需要进行相关权限设计. @PreAuthorize使 ...

  4. StringUtils的isBlank与isEmply

    1.public static boolean isEmpty(String str) 判断某字符串是否为空,为空的标准是 str==null 或 str.length()==0 StringUtil ...

  5. StringUtils中 isNotEmpty 和isNotBlank的区别

    isNotEmpty : 判断某字符串是否非空 StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = ...

  6. StringUtils工具类

    StringUtils源码,使用的是commons-lang3-3.1包.下载地址 http://commons.apache.org/lang/download_lang.cgi 以下是String ...

  7. StringUtils中的常用的方法

    org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法: 1. 检查字符串是否为空: static boolean isB ...

  8. StringUtils方法全集

    org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,是JDK提供的String类型操作方法的补充,并且是null安全的( ...

  9. StringUtils 的常用方法

    StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 ...

随机推荐

  1. c++ opencv 数学函数示例

    // ConsoleApplication11.cpp : Defines the entry point for the console application. // #include " ...

  2. css ~ a标签占满父级元素

    width: 100%; height: 100%; display: block;

  3. 大数据高可用集群环境安装与配置(09)——安装Spark高可用集群

    1. 获取spark下载链接 登录官网:http://spark.apache.org/downloads.html 选择要下载的版本 2. 执行命令下载并安装 cd /usr/local/src/ ...

  4. MD5碰撞和MD5值(哈希值)相等

    md5的碰撞 0e开头的md5和原值: s878926199a 0e545993274517709034328855841020 s155964671a 0e342768416822451524974 ...

  5. 谈谈我近一个半月的dp练习

    前请提示:https://www.cnblogs.com/caiyishuai/p/9047991.html   配合这篇文章食用风味更佳哦! 首先十分感谢henry_y提供的50道dp练习,链接在这 ...

  6. Spring Boot Actuator Endpoints

    常用内建的Endpoints: beans:显示当前Spring应用上下文的Spring Bean完整列表(包含所有ApplicationContext的层次) conditions:显示当前应用所有 ...

  7. HDU 5312:Sequence

    Sequence  Accepts: 25  Submissions: 1442  Time Limit: 2000/2000 MS (Java/Others)  Memory Limit: 2621 ...

  8. Java编译器 & Java解释器 & JVM

    转自:https://www.cnblogs.com/chengdabelief/p/6576320.html JVM JVM有自己完善的硬件架构,如处理器.堆栈(Stack).寄存器等,还具有相应的 ...

  9. Spring的配置文件说明

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  10. tfield的字段名和显示名

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...