代码拉取完成,页面将自动刷新
---
id: cc01
title: 1. Hutool工具类
sidebar_label: 1. Hutool工具类
description: 1. Hutool工具类
---
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,官网地址 https://www.hutool.cn/
下面罗列了 `vben` 框架常用到的 Hutool工具类以及一些相关扩展。
## 1.1 StrUtil 字符串工具类
### 1.1.1 判断空
<table>
<thead>
<tr>
<th width="220" align="center">方法名</th>
<th width="*">说明</th>
<th width="300" align="center">示例</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">isEmpty</td>
<td align="left">判断给定的字符串是否为空,这里的"空"指的是null或者空字符串(""),关联取反方法 isNotEmpty</td>
<td align="left">
StrUtil.isEmpty(null); // true <br/>
StrUtil.isEmpty(""); // true <br/>
StrUtil.isEmpty(" "); // false <br/>
</td>
</tr>
<tr>
<td align="center">hasEmpty</td>
<td align="left">是否包含空字符串</td>
<td align="left">
</td>
</tr>
<tr>
<td align="center">isBlank</td>
<td align="left">判断给定的字符串是否为空或者仅包含空白字符(如空格、制表符、换行符等)。关联取反方法 isNotBlank</td>
<td align="left">
StrUtil.isBlank(null); // true <br/>
StrUtil.isBlank(""); // true <br/>
StrUtil.isBlank(" "); // true <br/>
</td>
</tr>
<tr>
<td align="center">isAllNotBlank</td>
<td align="left">指定字符串数组中的元素,是否全部为空白字符串。</td>
<td align="left">
</td>
</tr>
</tbody>
</table>
### 1.1.2 判断相等
<table>
<thead>
<tr>
<th width="550">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>equals(CharSequence str1, CharSequence str2)</td>
<td>比较两个字符串(大小写敏感)</td>
</tr>
<tr>
<td>equalsIgnoreCase(CharSequence str1, CharSequence str2)</td>
<td>比较两个字符串(大小写不敏感)</td>
</tr>
<tr>
<td>equalsAnyIgnoreCase(CharSequence str1, CharSequence... strs)</td>
<td>
给定字符串是否与提供的中任一字符串相同(忽略大小写),相同则返回true,没有相同的返回false。
如果参与比对的字符串列表为空,返回false。
</td>
</tr>
<tr>
<td>equalsAny(CharSequence str1, CharSequence... strs)</td>
<td>给定字符串是否与提供的中任一字符串相同,相同则返回true,没有相同的返回false。
如果参与比对的字符串列表为空,返回false
</td>
</tr>
</tbody>
</table>
### 1.1.3 判断开始,结尾,包含
<table>
<thead>
<tr>
<th width="550">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>startWith(CharSequence str, CharSequence prefix)</td>
<td>是否以指定字符串开头,判断字符有重载方法</td>
</tr>
<tr>
<td>startWithIgnoreCase(CharSequence str, CharSequence prefix)</td>
<td>
是否以指定字符串开头,忽略大小写
</td>
</tr>
<tr>
<td>endWith(CharSequence str, CharSequence suffix)</td>
<td>
是否以指定字符串结尾,判断字符有重载方法
</td>
</tr>
<tr>
<td>endWithAny(CharSequence str, CharSequence... suffixes)</td>
<td>
给定字符串是否以任何一个字符串结尾, 给定字符串和数组为空都返回false
</td>
</tr>
<tr>
<td>contains(CharSequence str, CharSequence searchStr)</td>
<td>
指定字符串是否在字符串中出现过,判断字符有重载方法
</td>
</tr>
<tr>
<td>containsAny(CharSequence str, CharSequence... testStrs)</td>
<td>
查找指定字符串是否包含指定字符串列表中的任意一个字符串,判断字符有重载方法
</td>
</tr>
</tbody>
</table>
### 1.1.4 查找
<table>
<thead>
<tr>
<th width="550">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>indexOf(CharSequence str, char searchChar)</td>
<td>指定范围内查找指定字符,返回位置</td>
</tr>
<tr>
<td>indexOfIgnoreCase(final CharSequence str, final CharSequence searchStr)</td>
<td>指定范围内查找字符串,忽略大小写</td>
</tr>
</tbody>
</table>
### 1.1.5 移除
<table>
<thead>
<tr>
<th width="550">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>trim</td>
<td>除去字符串头尾部的空白,如果字符串是null,依然返回null</td>
</tr>
<tr>
<td>trimToEmpty</td>
<td>除去字符串头尾部的空白,如果字符串是null,返回空串""</td>
</tr>
<tr>
<td>removePrefix(CharSequence str, CharSequence prefix)</td>
<td>去掉指定前缀</td>
</tr>
<tr>
<td>removeAll(CharSequence str, CharSequence strToRemove)</td>
<td>
移除字符串中所有给定字符串。 <br/>
例:removeAll("aa-bb-cc-dd", "-") =》 aabbccdd
</td>
</tr>
</tbody>
</table>
### 1.1.6 子字符串
<table>
<thead>
<tr>
<th width="550">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>sub(CharSequence str, int fromIndexInclude, int toIndexExclude)</td>
<td>index从0开始计算,最后一个字符为-1。<br/>
如果from和to位置一样,返回 "" <br/>
如果from或to为负数,则按照length从后向前数位置,如果绝对值大于字符串长度,则from归到0,to归到length <br/>
如果经过修正的index中from大于to,则互换from和to <br/>
例:StrUtil.sub("abcdefgh",2,-3) =》cde 注意不包含f
</td>
</tr>
<tr>
<td>subWithLength(String input, int fromIndex, int length)</td>
<td>截取字符串,从指定位置开始,截取指定长度的字符串。<br/>
如果fromIndex为正数,则向后截取指定length长度,如果为负数,则向前截取length长度。
</td>
</tr>
<tr>
<td>subAfter(CharSequence string, CharSequence separator,
boolean isLastSeparator)
</td>
<td>截取分隔字符串之后的字符串,不包括分隔字符串。<br/>
如果给定的字符串为空串(null或""),返回原字符串。<br/>
如果分隔字符串为空串(null或""),则返回空串,如果分隔字符串未找到。
</td>
</tr>
<tr>
<td>subBetween(CharSequence str, CharSequence before, CharSequence after)</td>
<td>截取指定字符串中间部分,不包括标识字符串。<br/>
例: CharSequenceUtil.subBetween("wx[b]yz", "[", "]") = "b"
</td>
</tr>
</tbody>
</table>
### 1.1.7 替换
<table>
<thead>
<tr>
<th width="550">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>replace(CharSequence str, CharSequence searchStr,CharSequence replacement)</td>
<td>替换字符串中的指定字符串</td>
</tr>
<tr>
<td>replaceIgnoreCase(CharSequence str, CharSequence searchStr,
CharSequence replacement)
</td>
<td>替换字符串中的指定字符串,忽略大小写</td>
</tr>
</tbody>
</table>
### 1.1.8 大小写转换
<table>
<thead>
<tr>
<th width="550">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>lowerFirst(CharSequence str)</td>
<td>小写首字母</td>
</tr>
<tr>
<td>upperFirst(CharSequence str)</td>
<td>大写首字母</td>
</tr>
</tbody>
</table>
### 1.1.9 拆分
<table>
<thead>
<tr>
<th width="550">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>split(CharSequence str, CharSequence separator)</td>
<td>切分字符串,返回字符集合</td>
</tr>
<tr>
<td>splitTrim(CharSequence str, char separator)</td>
<td>切分字符串返回集合,去除切分后每个元素两边的空白符,去除空白项</td>
</tr>
<tr>
<td>splitToArray(CharSequence str, CharSequence separator)</td>
<td>切分字符串,返回数组</td>
</tr>
</tbody>
</table>
### 1.1.10 其他
<table>
<thead>
<tr>
<th width="550">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>blankToDefault(CharSequence cs)</td>
<td>如果字符串是空白字符串,则返回指定默认字符串,否则返回字符串本身。</td>
</tr>
<tr>
<td>str(CharSequence cs)</td>
<td>转为字符串,null安全</td>
</tr>
<tr>
<td>toUnderlineCase(CharSequence str)</td>
<td>将驼峰式命名的字符串转换为下划线方式。如果转换前的驼峰式命名的字符串为空,则返回空字符串。</td>
</tr>
<tr>
<td>format(CharSequence template, Object... params)</td>
<td>格式化文本, {} 表示占位符 <br/>
此方法只是简单将占位符 {} 按照顺序替换为参数 <br/>
例: <br/>
通常使用:format("this is {} for {}", "a", "b") =》 this is a for b <br/>
转义{}: format("this is \{} for {}", "a", "b") =》 this is {} for a <br/>
转义\: format("this is \\{} for {}", "a", "b") =》 this is \a for b
</td>
</tr>
<tr>
<td>join(CharSequence conjunction, Object... objs)</td>
<td>以 conjunction 为分隔符将多个对象转换为字符串</td>
</tr>
<tr>
<td>padPre(CharSequence str, int length, char padChar)</td>
<td>补充字符串(往前补充)以满足最小长度,如果提供的字符串大于指定长度,截断之。往后则用padAfter</td>
</tr>
</tbody>
</table>
## 1.2 DateUtil 日期工具类
<table>
<thead>
<tr>
<th width="350">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>parse(CharSequence dateCharSequence)</td>
<td>字符串转日期。<br/>
String dateStr = "2017-03-01";<br/>
Date date = DateUtil.parse(dateStr);
</td>
</tr>
<tr>
<td>format(Date date, String format)</td>
<td>格式化日期输出<br/>
String dateStr = "2017-03-01"; <br/>
Date date = DateUtil.parse(dateStr); <br/>
String format = DateUtil.format(date, "yyyy/MM/dd"); //结果 2017/03/01
</td>
</tr>
<tr>
<td>betweenDay(Date beginDate, Date endDate, boolean isReset) </td>
<td>计算两个日期之间的时间差<br/>
String dateStr1 = "2017-03-01 22:33:23"; <br/>
Date date1 = DateUtil.parse(dateStr1); <br/>
String dateStr2 = "2017-04-01 23:33:23"; <br/>
Date date2 = DateUtil.parse(dateStr2); <br/>
long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);//相差一个月,31天
</td>
</tr>
</tbody>
</table>
## 1.2 IdUtil 唯一ID工具
<table>
<thead>
<tr>
<th width="350">方法名</th>
<th width="*">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td>simpleUUID</td>
<td>生成不带-的32位UUID字符串<br/>
类似于:b17f24ff026d40949c85a24f4f375d42
</td>
</tr>
<tr>
<td>getSnowflakeNextId</td>
<td>生成大整型的雪花ID<br/>
类似于:1990390540053430272
</td>
</tr>
<tr>
<td>getSnowflakeNextIdStr</td>
<td>生成大整型的雪花ID 字符串格式<br/>
</td>
</tr>
</tbody>
</table>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。