当前位置: 首页 > news >正文

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

简介

在我们的代码中经常需要对字符串判空,截取字符串、转换大小写、分隔字符串、比较字符串、去掉多余空格、拼接字符串、使用正则表达式等等。如果只用 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);}
}
http://www.lryc.cn/news/284114.html

相关文章:

  • 设计模式 代理模式(静态代理 动态代理) 与 Spring Aop源码分析 具体是如何创建Aop代理的
  • 【EI会议征稿通知】第七届先进电子材料、计算机与软件工程国际学术会议(AEMCSE 2024)
  • Verilog基础:强度建模(一)
  • Spring Boot各类变量的使用
  • Hive管理UDF详解
  • bug笔记:解决 HTTP Error 500.30 - ASP.NET Core app failed to start
  • 理解pytorch系列:transpose是怎么实现的
  • Linux tftp命令教程:文件传输利器(附案例详解和注意事项)
  • beego的模块篇 - task任务
  • ThreadLocal工具类
  • 【c语言】扫雷(上)
  • Java读取制表符文本转换为JSON
  • 从C到C++:向面向对象过渡的技巧与诀窍
  • Vue3中动态组件使用
  • kubernetes工作负载-DamonSet
  • zabbix其他配置
  • 蓝桥杯备战 每日一题 (2)
  • GetShell的姿势
  • workflow源码解析:ThreadTask
  • 为何谷歌强制要求安装ssl证书?
  • 【刷题】 leetcode 2 .两数相加
  • Webpack5入门到原理2:基本使用
  • 企业微信上传临时素材errcode:44001,errmsg:empty media data
  • Docker技巧汇总
  • 学习使用微信小程序实现智能名片电子名片功能代码
  • 学习响应式编程中遇到的奇奇怪怪的问题
  • 前端常用js、css效果
  • Modern C++ 条件变量
  • 免费chartGPT网站汇总--
  • 关于C#中的async/await的理解