字符串工具-StrUtil
这个工具的用处类似于Apache Commons Lang中的StringUtil。
1.hasBlank、hasEmpty方法
就是给定一些字符串,如果一旦有空的就返回true,常用于判断好多字段是否有空的(例如web表单数据)。
这两个方法的区别是hasEmpty只判断是否为null或者空字符串(””),hasBlank则会把不可见字符也算做空,isEmpty和isBlank同理。
/*** 基础用法-判断是否为空*/@Testpublic void isnull() {String str = "www.qingfeng.plus";//基础功能System.out.println(StrUtil.isBlank(str));System.out.println(StrUtil.isNotBlank(str));System.out.println(StrUtil.isEmpty(str));System.out.println(StrUtil.isNotEmpty(str));//isBlank判断某字符串是否为空或长度为0或由空白符(whitespace)构成System.out.println("===============isBlank===============");System.out.println(StrUtil.isBlank(""));System.out.println(StrUtil.isBlank(null));System.out.println(StrUtil.isBlank(" "));System.out.println(StrUtil.isBlank("abc"));//对于制表符、换行符、换页符和回车符StringUtils.isBlank()均识为空白符System.out.println(StrUtil.isBlank("\t"));System.out.println(StrUtil.isBlank("\r"));System.out.println(StrUtil.isBlank("\n"));System.out.println(StrUtil.isBlank("\f"));//\b为单词边界符System.out.println(StrUtil.isBlank("\b"));//判断某字符串是否为空,为空的标准是str==null或str.length()==0System.out.println("===============isEmpty===============");System.out.println(StrUtil.isEmpty(""));System.out.println(StrUtil.isEmpty(" "));System.out.println(StrUtil.isEmpty("abc"));//对于制表符、换行符、换页符和回车符StringUtils.isEmpty()均识为非空字符串System.out.println(StrUtil.isEmpty("\t"));System.out.println(StrUtil.isEmpty("\r"));System.out.println(StrUtil.isEmpty("\n"));System.out.println(StrUtil.isEmpty("\f"));//\b为单词边界符System.out.println(StrUtil.isEmpty("\b"));}
2.removePrefix、removeSuffix方法
这两个是去掉字符串的前缀后缀的,例如去个文件名的扩展名啥。
还有忽略大小写的removePrefixIgnoreCase和removeSuffixIgnoreCase都比较实用。
@Testpublic void remove() {String name = "www.qingfeng.plus";String fileName = StrUtil.removeSuffix(name, ".plus"); //fileName -> qingfengString suffix = StrUtil.removePrefix(name,"www.");System.out.println(fileName);System.out.println(suffix);}
3.sub方法
有人说String有了subString你还写它干啥,我想说subString方法越界啥的都会报异常。
index的位置还支持负数哦,-1表示最后一个字符
String str = "abcdefgh";String strSub1 = StrUtil.sub(str, 2, 3); //strSub1 -> cString strSub2 = StrUtil.sub(str, 2, -3); //strSub2 -> cdeString strSub3 = StrUtil.sub(str, 3, 2); //strSub2 -> c
4.format方法
参数我定义成了Object类型,如果传别的类型的也可以,会自动调用toString()方法的。
String template = "{}爱{},就像老鼠爱大米";String str = StrUtil.format(template, "我", "你"); //str -> 我爱你,就像老鼠爱大米
16进制工具-HexUtil
16进制一般针对无法显示的一些二进制进行显示,常用于: 1、图片的字符串表现形式 2、加密解密 3、编码转换
HexUtil主要以encodeHex和decodeHex两个方法为核心,提供一些针对字符串的重载方法。
@Testpublic void HexUtil() {String str = "www.qingfeng.plus";String hex = HexUtil.encodeHexStr(str, CharsetUtil.CHARSET_UTF_8);System.out.println(hex);//hex是://7777772e71696e6766656e672e706c7573String decodedStr = HexUtil.decodeHexStr(hex);//解码后与str相同System.out.println(decodedStr);}
Escape工具-EscapeUtil
此类中的方法对应Javascript中的escape()函数和unescape()函数。
- EscapeUtil.escape Escape编码(Unicode),该方法不会对 ASCII 字母和数字进行编码,也不会对下面这些 ASCII 标点符号进行编码: * @ - _ + . / 。其他所有的字符都会被转义序列替换。
- EscapeUtil.unescape Escape解码。
EscapeUtil.safeUnescape 安全的unescape文本,当文本不是被escape的时候,返回原文。
@Testpublic void EscapeUtil() {String str = "www.qingfeng.plus?name=张三";String escapeStr = EscapeUtil.escape(str);//基础功能System.out.println(escapeStr);System.out.println(EscapeUtil.unescape(escapeStr));System.out.println(EscapeUtil.safeUnescape(str));/*** www.qingfeng.plus%3fname%3d%u5f20%u4e09* www.qingfeng.plus?name=张三* www.qingfeng.plus?name=张三*/}
Hash算法-HashUtil
HashUtil其实是一个hash算法的集合,此工具类中融合了各种hash算法。 :::info
additiveHash 加法hash
- rotatingHash 旋转hash
- oneByOneHash 一次一个hash
- bernstein Bernstein’s hash
- universal Universal Hashing
- zobrist Zobrist Hashing
- fnvHash 改进的32位FNV算法1
- intHash Thomas Wang的算法,整数hash
- rsHash RS算法hash
- jsHash JS算法
- pjwHash PJW算法
- elfHash ELF算法
- bkdrHash BKDR算法
- sdbmHash SDBM算法
- djbHash DJB算法
- dekHash DEK算法
- apHash AP算法
- tianlHash TianL Hash算法
- javaDefaultHash JAVA自己带的算法
- mixHash 混合hash算法,输出64位的值
:::
URL工具-URLUtil
- URLUtil.url 通过一个字符串形式的URL地址创建对象
- URLUtil.getURL 主要获得ClassPath下资源的URL,方便读取Classpath下的配置文件等信息。
- URLUtil.normalize 标准化化URL链接。对于不带http://头的地址做简单补全。 ```basic String url = “http://www.qingfeng.plus//aaa/bbb“; // 结果为:http://www.qingfeng.plus/aaa/bbb String normalize = URLUtil.normalize(url);
url = “http://www.qingfeng.plus//aaa/\\bbb?a=1&b=2“; // 结果为:http://www.qingfeng.plus/aaa/bbb?a=1&b=2 normalize = URLUtil.normalize(url);
- URLUtil.encode 封装URLEncoder.encode,将需要转换的内容(ASCII码形式之外的内容),用十六进制表示法转换出来,并在之前加上%开头。```basicString body = "366466 - 副本.jpg";// 结果为:366466%20-%20%E5%89%AF%E6%9C%AC.jpgString encode = URLUtil.encode(body);
- URLUtil.decode 封装URLDecoder.decode,将%开头的16进制表示的内容解码。
- URLUtil.getPath 获得path部分 URI -> http://www.aaa.bbb/search?scope=ccc&q=ddd PATH -> /search
- URLUtil.toURI 转URL或URL字符串为URI。
XML工具-XmlUtil
在日常编码中,我们接触最多的除了JSON外,就是XML格式了,一般而言,我们首先想到的是引入Dom4j包,却不知JDK已经封装有XML解析和构建工具:w3c dom。但是由于这个API操作比较繁琐,因此Hutool中提供了XmlUtil简化XML的创建、读和写的过程。
读取XML
读取XML分为两个方法:
- XmlUtil.readXML 读取XML文件
- XmlUtil.parseXml 解析XML字符串为Document对象
