Java 中的 StringUtils
本文介绍什么是 StringUtils
以及如何在 Java 中使用它来处理字符串。
StringUtils
是一个用于处理 String 的类,它提供了比 Java String
类更多的实用方法。该类不属于 Java 包;相反,它属于 Apache Commons Library
。
要在你的包中使用这个类,你必须首先在你的项目中包含它的 JAR 文件,然后在你的 Java 源代码中导入 StringUtils
类。
如果你正在使用 maven 项目,请在 pom.xml
文件中使用以下依赖项。它会将所需的 JAR 添加到你当前的项目中。
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
这个类可以在 org.apache.commons.lang3
包中找到,这个类的签名如下所示。
StringUtils
类的语法:
public class StringUtils extends Object
StringUtils
类的构造函数:
StringUtils() // no arg constructor
以下是 StringUtils
支持的一些常见操作。
操作 | 说明 |
---|---|
IsEmpty /IsBlank |
它检查字符串是否包含文本。 |
Trim /Strip |
它删除字符串的前导和尾随空格。 |
Equals /Compare |
它以空安全的方式比较两个字符串。 |
startsWith |
它检查字符串是否以空安全方式以前缀开头。 |
endsWith |
它检查字符串是否以空安全方式以后缀结尾。 |
IndexOf /LastIndexOf /Contains |
它返回一个空安全的检查索引。 |
IndexOfAny /LastIndexOfAny/IndexOfAnyBut /LastIndexOfAnyBut |
查找任何一组字符串的索引。 |
ContainsOnly /ContainsNone/ContainsAny |
它检查字符串是否只包含/不包含/任何这些字符 |
Split /Join |
它将字符串拆分为子字符串数组,反之亦然。 |
Remove /Delete |
它删除了字符串的一部分。 |
Java 中的 StringUtils
示例
在本例中,我们使用了 StringUtils
类的一些标准方法来了解该类如何使用 String。此类类似于 Java 中的 String
类,不同之处在于它提供了更多使用 String 的实用方法。
请参见下面的示例。
import org.apache.commons.lang3.StringUtils;
public class SimpleTesting {
public static void main(String[] args) {
// Get abbreviation of string
String val = StringUtils.abbreviate("Delft", 4);
System.out.println(val);
// Capitalize string
val = StringUtils.capitalize("delft");
System.out.println(val);
// Chop string
val = StringUtils.chop("delft");
System.out.println(val);
// Compare two strings
int a = StringUtils.compare(val, val);
System.out.println(a);
// find substring in string
boolean b = StringUtils.contains("delft", "ft");
System.out.println(b);
// Find index of a char in string
int c = StringUtils.indexOf(val, 'f');
System.out.println(c);
// Find last index of a char in string
int d = StringUtils.lastIndexOf("delftstack", 't');
System.out.println(d);
// Lowercase string
val = StringUtils.lowerCase("DELFSTACK");
System.out.println(val);
// Repeat string
val = StringUtils.repeat("DELF", 2);
System.out.println(val);
// Reverse string
val = StringUtils.reverse("Delft");
System.out.println(val);
// Truncate string
val = StringUtils.truncate("Delft", 2);
System.out.println(val);
// Uppercase string
val = StringUtils.upperCase("Delft");
System.out.println(val);
}
}
输出:
D...
Delft
delf
0
true
3
6
delfstack
DELFDELF
tfleD
De
DELFT
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Pandas 的列中展平层次索引
发布时间:2024/04/24 浏览次数:1782 分类:Python
-
在这篇文章中,我们将使用不同的函数来使用 Pandas DataFrame 列来展平层次索引。我们将使用的方法是重置索引和 as_index() 函数。
计算 Pandas DataFrame 中的方差
发布时间:2024/04/23 浏览次数:212 分类:Python
-
本教程演示了如何计算 Python Pandas DataFrame 中的方差。
Pandas 中的 Groupby 索引列
发布时间:2024/04/23 浏览次数:89 分类:Python
-
本教程将介绍如何使用 Python Pandas Groupby 对数据进行分类,然后将函数应用于类别。通过示例使用 groupby() 函数按 Pandas 中的多个索引列进行分组。