Apache StringUtils:Java字符串处理工具类

本文介绍了ApacheCommonsLang3库中的StringUtils工具类,提供了一套全面的字符串处理功能,包括判空、截取、转换、比较、去除空格等,简化了Java开发中常见的字符串操作。
摘要由CSDN通过智能技术生成

简介

在我们的代码中经常需要对字符串判空,截取字符串、转换大小写、分隔字符串、比较字符串、去掉多余空格、拼接字符串、使用正则表达式等等。如果只用 String 类提供的那些方法,我们需要手写大量的额外代码,不然容易出现各种异常。现在有个好消息是:org.apache.commons.lang3包下的StringUtils工具类,给我们提供了非常丰富的选择。在这里插入图片描述

Maven依赖:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

方法列表和描述

  • IsEmpty/IsBlank - 检查字符串是否包含文本
  • Trim/Strip - 移除字符串的前导和尾随空白
  • Equals/Compare - 以空安全的方式比较两个字符串
  • startsWith - 以空安全的方式检查字符串是否以指定前缀开头
  • endsWith - 以空安全的方式检查字符串是否以指定后缀结尾
  • IndexOf/LastIndexOf/Contains - 空安全的索引检查
  • IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - 在一组字符串中查找任意字符串的索引
  • ContainsOnly/ContainsNone/ContainsAny - 检查字符串是否只包含/不包含/包含任意一组字符
  • Substring/Left/Right/Mid - 空安全的子字符串提取
  • SubstringBefore/SubstringAfter/SubstringBetween - 相对于其他字符串的子字符串提取
  • Split/Join - 将字符串拆分为子字符串数组,反之亦然
  • Remove/Delete - 移除字符串的部分内容
  • Replace/Overlay - 在字符串中搜索并用另一个字符串替换
  • Chomp/Chop - 移除字符串的最后一部分
  • AppendIfMissing - 如果不存在,将后缀追加到字符串的末尾
  • PrependIfMissing - 如果不存在,将前缀添加到字符串的开头
  • LeftPad/RightPad/Center/Repeat - 填充字符串
  • UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - 更改字符串的大小写
  • CountMatches - 计算一个字符串在另一个字符串中出现的次数
  • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - 检查字符串中的字符
  • DefaultString - 防止空输入字符串
  • Rotate - 旋转(循环移位)字符串
  • Reverse/ReverseDelimited - 反转字符串
  • Abbreviate - 使用省略号或另一个给定的字符串缩写字符串
  • Difference - 比较字符串并报告它们之间的差异
  • LevenshteinDistance - 将一个字符串更改为另一个所需的更改次数

empyt和blank都是判空有什么区别:
" " isEmpty 返回false;isBlank返回true

一些常用的字符串常量:
在这里插入图片描述

使用DEMO

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

import org.apache.commons.lang3.StringUtils;

public class StringUtilsDemo {

    public static void main(String[] args) {
        String str1 = "Hello, World!";
        String str2 = "";

        // 判断字符串是否为空或者空白
        System.out.println("Is str1 empty or blank? " + StringUtils.isBlank(str1));
        System.out.println("Is str2 empty or blank? " + StringUtils.isBlank(str2));
    }
}

2 连接多个字符串:

import org.apache.commons.lang3.StringUtils;

public class StringUtilsDemo {

    public static void main(String[] args) {
        String[] words = {"Hello", "World", "Java"};

        // 连接多个字符串
        String result = StringUtils.join(words, " ");
        System.out.println("Result: " + result);
    }
}

截取字符串的前几个字符:

import org.apache.commons.lang3.StringUtils;

public class StringUtilsDemo {

    public static void main(String[] args) {
        String original = "Apache StringUtils Demo";

        // 截取字符串的前几个字符
        String substring = StringUtils.left(original, 10);
        System.out.println("Substring: " + substring);
    }
}

4 移除字符串中的空格:

import org.apache.commons.lang3.StringUtils;

public class StringUtilsDemo {

    public static void main(String[] args) {
        String stringWithSpaces = "   Remove   Spaces   ";

        // 移除字符串中的空格
        String result = StringUtils.deleteWhitespace(stringWithSpaces);
        System.out.println("Result: " + result);
    }
}
  • 37
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值