专业的JAVA编程教程与资源

网站首页 > java教程 正文

Hutool工具包-字符串工具介绍(hutool工具类常用方法)

temp10 2024-11-10 11:33:36 java教程 11 ℃ 0 评论

Hutool是一个Java工具库,提供了丰富的工具类和方法,用于简化Java开发过程中的常见任务。它包含了各种功能模块,如字符串处理、日期时间操作、文件操作、加密解密、网络请求、Excel操作等。Hutool的设计目标是简单易用、高效可靠,提供了很多方便的工具方法,使得Java开发更加便捷和高效。无论是初学者还是有经验的开发人员,都可以受益于Hutool工具库的使用。

Hutool细分了以下几个工具模块:

Hutool工具包-字符串工具介绍(hutool工具类常用方法)

  • Core模块:提供了一些核心的工具类,如字符串处理、日期时间操作、对象工具等;
  • Aop模块:实现了简单的AOP(面向切面编程)功能,可以方便地进行方法拦截和增强;
  • Cache模块:提供了简单易用的缓存管理功能,支持多种缓存实现,如Ehcache、Redis等;
  • Crypto模块:封装了常见的加密解密算法,如MD5、AES、RSA等,方便进行数据加密和解密操作;
  • File模块:提供了文件操作相关的工具类,如文件读写、文件拷贝、文件压缩等;
  • Http模块:封装了HTTP请求和响应的相关操作,可以方便地进行HTTP请求、文件上传下载等操作;
  • Json模块:提供了JSON数据的解析和生成功能,支持多种JSON库,如Jackson、Fastjson等;
  • Excel模块:封装了Excel文件的读写操作,支持读取和写入Excel文件,以及Excel数据的转换和处理;
  • Email模块:提供了发送邮件的功能,支持SMTP和SSL等协议,可以方便地发送邮件;
  • Cron模块:封装了Cron表达式的解析和生成功能,可以方便地进行定时任务的管理。

引入:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>{hutool-version}</version>
</dependency>

Core模块

Hutool工具包的Core模块包含以下工具类:

  • StrUtil:字符串工具类,提供字符串操作的方法,如拼接、切割、修剪、填充、大小写转换等。
  • NumberUtil:数字工具类,提供数字格式化、随机数生成等方法。
  • DateUtil:日期和时间工具类,提供日期格式化、计算持续时间等方法。
  • ArrayUtil:数组工具类,提供对数组的操作方法,如排序、搜索等。
  • ObjectUtil:对象工具类,提供对象操作的方法,如克隆、比较、空检查等。
  • RandomUtil:随机工具类,提供生成随机数、字符串、颜色等方法。
  • IdUtil:ID工具类,提供生成各种ID的方法,如Snowflake ID、UUID等。
  • ZipUtil:Zip工具类,提供压缩和解压zip文件方法。
  • ImageUtil:图像工具类,提供缩放、旋转、翻转等图像方法。
  • XmlUtil:XML工具类,提供解析XML字符串和文件方法。
  • CollectionUtil:集合工具类,提供集合操作方法,如过滤、提取等。
  • BeanUtil:Bean工具类,提供Bean操作和反射方法。
  • ReflectUtil:反射工具类,提供简化反射操作的方法。
  • EncodeUtil:编码工具类,提供字符串编码和解码方法。
  • CharUtil:字符工具类,提供字符操作方法。

今天先搬运一下StrUtil工具类的用法。

1. 判断字符串是否为空或空白字符:

  • isEmpty(CharSequence str) :判断字符串是否为空,包括null和空字符串。
  • isBlank(CharSequence str) :判断字符串是否为空或空白字符,包括null、空字符串和只包含空白字符的字符串。

示例代码:

import cn.hutool.core.util.StrUtil;

public class MyClass {
    public static void main(String[] args) {
        String str1 = ""; // 空字符串
        String str2 = null; // null
        String str3 = "  "; // 空白字符
        
        System.out.println(StrUtil.isEmpty(str1)); // 输出:true
        System.out.println(StrUtil.isEmpty(str2)); // 输出:true
        System.out.println(StrUtil.isBlank(str3)); // 输出:true
    }
}

2. 字符串反转:

  • reverse(CharSequence str) :将字符串反转。

示例代码:

import cn.hutool.core.util.StrUtil;

public class MyClass {
    public static void main(String[] args) {
        String str = "Hello, Hutool!";
        String result = StrUtil.reverse(str);
        System.out.println(result); // 输出:!lootuH ,olleH
    }
}

3. 字符串拼接:

  • concat(CharSequence... strs) :将多个字符串拼接为一个字符串。
  • join(CharSequence delimiter, CharSequence... strs) :使用指定的分隔符将多个字符串拼接为一个字符串。

示例代码:

import cn.hutool.core.util.StrUtil;

public class MyClass {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "Hutool";
        
        String result1 = StrUtil.concat(str1, str2);
        System.out.println(result1); // 输出:HelloHutool
        
        String result2 = StrUtil.join(",", str1, str2);
        System.out.println(result2); // 输出:Hello,Hutool
    }
}

4. 字符串切割和拼接:

  • split(CharSequence str, char separator) :使用指定的字符分割字符串,并返回分割后的字符串数组。
  • split(CharSequence str, CharSequence separator) :使用指定的字符串分割字符串,并返回分割后的字符串数组。
  • split(CharSequence str, int len) :将字符串按指定长度进行分割,并返回分割后的字符串数组。
  • appendIfMissing(CharSequence str, CharSequence suffix) :如果字符串末尾不包含指定的后缀,则将后缀追加到字符串末尾。

示例代码:

import cn.hutool.core.util.StrUtil;

public class MyClass {
    public static void main(String[] args) {
        String str = "Hello,World";
        
        String[] split1 = StrUtil.split(str, ',');
        System.out.println(split1[0]); // 输出:Hello
        System.out.println(split1[1]); // 输出:World
        
        String[] split2 = StrUtil.split(str, ",");
        System.out.println(split2[0]); // 输出:Hello
        System.out.println(split2[1]); // 输出:World
        
        String[] split3 = StrUtil.split(str, 5);
        System.out.println(split3[0]); // 输出:Hello,
        System.out.println(split3[1]); // 输出:World
        
        String newStr = StrUtil.appendIfMissing(str, "!");
        System.out.println(newStr); // 输出:Hello,World!
    }
}

5. 字符串格式化:

  • format(CharSequence template, Object... values) :使用占位符将参数格式化为字符串。

示例代码:

import cn.hutool.core.util.StrUtil;

public class MyClass {
    public static void main(String[] args) {
        String template = "Hello, {}!";
        String name = "Hutool";
        
        String result = StrUtil.format(template, name);
        System.out.println(result); // 输出:Hello, Hutool!
    }
}

6. 字符串修剪和填充:

  • trim(CharSequence str) :删除字符串首尾的空白字符。
  • padPre(CharSequence str, int minLength, char padChar) :使用指定的字符填充字符串开头,确保最小长度。
  • padAfter(CharSequence str, int minLength, char padChar) :使用指定的字符填充字符串末尾,确保最小长度。

示例代码:

import cn.hutool.core.util.StrUtil;

public class MyClass {
    public static void main(String[] args) {
        String str = "   Hello, Hutool!   ";
        
        String trimmed = StrUtil.trim(str);
        System.out.println(trimmed); // 输出:"Hello, Hutool!"
        
        String paddedPre = StrUtil.padPre(str, 20, '*');
        System.out.println(paddedPre); // 输出:"****Hello, Hutool!   "
        
        String paddedAfter = StrUtil.padAfter(str, 20, '*');
        System.out.println(paddedAfter); // 输出:"   Hello, Hutool!****"
    }
}

7. 大小写转换:

  • toCamelCase(CharSequence str) :将字符串转换为驼峰式。
  • toUnderlineCase(CharSequence str) :将字符串转换为下划线式。

示例代码:

import cn.hutool.core.util.StrUtil;

public class MyClass {
    public static void main(String[] args) {
        String str = "hello_world";
        
        String camelCase = StrUtil.toCamelCase(str);
        System.out.println(camelCase); // 输出:"helloWorld"
        
        String underlineCase = StrUtil.toUnderlineCase(camelCase);
        System.out.println(underlineCase); // 输出:"hello_world"
    }
}

8. 字符串查找和替换:

  • indexOf(CharSequence str, CharSequence searchStr) :返回searchStr在str中首次出现的索引,如果不存在则返回-1。
  • lastIndexOf(CharSequence str, CharSequence searchStr) :返回searchStr在str中最后一次出现的索引,如果不存在则返回-1。
  • contains(CharSequence str, CharSequence searchStr) :判断str是否包含searchStr,如果包含则返回true,否则返回false。
  • replace(CharSequence str, CharSequence searchStr, CharSequence replaceStr) :将str中所有的searchStr替换为replaceStr,并返回替换后的字符串。

示例代码:

import cn.hutool.core.util.StrUtil;

public class MyClass {
    public static void main(String[] args) {
        String str = "Hello, Hutool!";
        
        int index = StrUtil.indexOf(str, "Hutool");
        System.out.println(index); // 输出:7
        
        int lastIndex = StrUtil.lastIndexOf(str, "l");
        System.out.println(lastIndex); // 输出:10
        
        boolean contains = StrUtil.contains(str, "Hutool");
        System.out.println(contains); // 输出:true
        
        String replaced = StrUtil.replace(str, "Hutool", "Tool");
        System.out.println(replaced); // 输出:Hello, Tool!
    }
}

9. 其他方法:

  • equals(CharSequence str1, CharSequence str2) :判断两个字符串是否相等,区分大小写。
  • equalsIgnoreCase(CharSequence str1, CharSequence str2) :判断两个字符串是否相等,忽略大小写。
  • startWith(CharSequence str, CharSequence prefix) :判断字符串是否以指定前缀开头。
  • endWith(CharSequence str, CharSequence suffix) :判断字符串是否以指定后缀结尾。
  • upperFirst(CharSequence str) :将字符串的首字母转换为大写。

示例代码:

import cn.hutool.core.util.StrUtil;

public class MyClass {
    public static void main(String[] args) {
        String str1 = "Hello";
        String str2 = "hello";
        
        boolean equals = StrUtil.equals(str1, str2);
        System.out.println(equals); // 输出:false
        
        boolean equalsIgnoreCase = StrUtil.equalsIgnoreCase(str1, str2);
        System.out.println(equalsIgnoreCase); // 输出:true
        
        boolean startWith = StrUtil.startWith(str1, "He");
        System.out.println(startWith); // 输出:true
        
        boolean endWith = StrUtil.endWith(str1, "lo");
        System.out.println(endWith); // 输出:true
        
        String upperFirst = StrUtil.upperFirst(str2);
        System.out.println(upperFirst); // 输出:Hello
    }
}

以上这些是Hutool工具包Core模块中字符串工具类(StrUtil)提供的一些其他功能示例。

其它待续。


本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表