StringUtils 简介
StringUtils是最常用的 Apache Commons 类。它包含各种实用程序和方法,可帮助开发人员避免编写样板文件或简单地为基本操作编写笨拙的代码。
StringUtils类中的许多方法都有它们的java.lang.String等效方法,但与java.lang.String方法不同,它们是空值安全的。这意味着不会在最意外的时刻抛出NullPointerException 。
Apache Commons 包含许多方法,我们将介绍一些最常用的方法。
StringUtils 方法列表:
| 是空的() | 检查字符串是否为空 |
| 等于() | 比较字符串 |
| 比较() | 比较字符串 |
| 指数() | 在字符串中查找子字符串 |
| 最后索引() | 在字符串中查找子字符串 |
| 包含() | 检查子字符串是否在字符串中 |
| containsIgnoreCase() | 检查字符串中是否出现子字符串,忽略大小写 |
| containsAny() | 检查子字符串是否出现在字符串中的任何位置 |
| containsNone() | 检查子字符串是否出现在字符串中的任何位置 |
| 仅包含() | 检查子字符串是否在字符串中 |
| 子串() | 获取子串 |
| 分裂() | 将字符串拆分为子字符串 |
| 加入() | 连接子串 |
| 消除() | 删除子字符串 |
| 代替() | 替换子串 |
| 计数匹配() | 计算匹配次数 |
StringUtils.isEmpty() 和 StringUtils.isBlank()
这两种方法都用于检查字符串是否包含任何文本。如果字符串真的是空的,它们返回 true。此外,如果字符串仅包含空格,isBlank()也将返回true 。
它们也有自己的逆向方法:isNotEmpty()和isNotBlank()。
让我们看看如何使用isEmpty()及其对应的java.lang.String.isEmpty()以及isBlank():
String nullValue = null;
String emptyValue = "";
String blankValue = "\n \t \n";
if(StringUtils.isEmpty(emptyValue)) {
System.out.println("emptyValue is emptyValue");
}
if(StringUtils.isBlank(blankValue)) {
System.out.println("blankValue is blankValue");
}
if(!nullValue.isEmpty()) {
System.out.println("nullString isn't null");
}
这里有三个String类型的变量。一个指向null,第二个不为空但没有内容(空字符串),第三个不为空但将打印空结果。
运行此代码会导致:
emptyValue is emptyValue
blankValue is blankValue
Exception in thread "main" java.lang.NullPointerException
java.lang.String中内置的isEmpty()方法不是空安全的。如果您尝试检查它是否为空,您将很容易得到NullPointerException ,因为您在null reference 上调用了该方法。有必要提前检查引用是否为空:
String nullValue = null;
String emptyValue = "";
String blankValue = "\n \t \n";
if(StringUtils.isEmpty(emptyValue)) {
System.out.println("emptyValue is emptyValue");
}
if(StringUtils.isBlank(blankValue)) {
System.out.println("blankValue is blankValue");
}
if(nullValue != null && !nullValue.isEmpty()) {
System.out.println("nullString isn't null");
}
现在这导致:
emptyValue is emptyValue
blankValue is blankValue
如果我们测试这些方法空字符串:
String nullValue = null;
if(StringUtils.isEmpty(nullValue)) {
System.out.println("nullValue is emptyValue");
}
if(StringUtils.isBlank(nullValue)) {
System.out.println("nullValue is blankValue");
}
然后我们得到:
nullValue is emptyValue
nullValue is blankValue
StringUtils方法是null安全的,即使传递null也能产生预期的结果。
StringUtils.equals()
此方法比较两个字符串,如果它们相同或两个引用都指向null则返回true,但请注意此方法区分大小写。
让我们看看它是如何工作的:
System.out.println(StringUtils.equals(null, null));
System.out.println(StringUtils.equals(null, "some information"));
System.out.println(StringUtils.equals("some information", null));
System.out.println(StringUtils.equals("some information", "some information"));
System.out.println(StringUtils.equals("some additional information", "some information"));
结果:
true
false
false
true
false
将StringUtils中的equals()方法与java.lang.String.equals()进行比较:
String nullValue = null;
System.out.println(StringUtils.equals(nullValue, null));
System.out.println(StringUtils.equals(nullValue, "some information"));
System.out.println(nullValue.equals(null));
System.out.println(nullValue.equals("some information"));
这使您回到:
true
false
Exception in thread "main" java.lang.NullPointerException
同样,在空引用上调用方法会导致NullPointerException,您需要在使用引用变量之前检查它是否为空。
StringUtils.compare()
此方法的声明如下所示:
public static int compare(final String str1, final String str2)
此方法按字典顺序比较两个字符串,就像java.lang.String.compareTo()方法所做的那样,返回:
- 0 如果 str1 等于 str2(或两者都为空)
- 如果 str1 小于 str2,则该值小于 0
- 如果 str1 大于 str2,则值大于 0
字典序就是字典序。让我们看看如何在我们的程序中使用它:
System.out.println(StringUtils.compare(null, null));
System.out.println(StringUtils.compare(null , "codeGym"));
System.out.println(StringUtils.compare("codeGym", null));
System.out.println(StringUtils.compare("codeGym", "CODEGYM"));
System.out.println(StringUtils.compare("codeGym", "codeGym"));
我们得到:
0
-1
1
32
0
注意:空值被认为小于非空值。两个空值被认为是相等的。
检查一个字符串是否包含另一个子字符串
为此,StringUtils有 5 个方法:
- 包含()
- containsIgnoreCase()
- containsAny()
- containsNone()
- 仅包含()
contains()方法根据搜索序列是否包含在另一个序列中返回true或false 。
如果将null传递给这样的方法,它将返回false。如果传递了非null ,该方法将简单地在传递的对象上调用java.lang.String.indexOf(String str) 。
例子:
String value = "CodeGym is cool";
System.out.println(StringUtils.contains(null, "a"));
System.out.println(StringUtils.contains(value, "CodeGym"));
System.out.println(StringUtils.contains(value, "C++"));
System.out.println(StringUtils.contains(value, "codegym"));
该方法区分大小写,因此最后一次调用也将返回false:
false
true
false
false
如果作为第一个参数传递的字符串包含至少一个在 2-N 个参数中传递的子字符串,则containsAny()方法返回true 。
例子:
String value = "CodeGym is cool";
System.out.println(StringUtils.containsAny(value, "cool", "c00l", "bro", "hello"));
会显示:
true
此方法也区分大小写。
containsNone() 方法
当您需要检查某个字符串是否不包含列表中的任何内容时,您可以使用containsNone()方法。第一个参数是一个字符串,后面的参数是不应该在目标接收器中的字符串。
例子:
String s = "CodeGym is cool";
System.out.println(StringUtils.containsNone(s, 'g', 'a'));
控制台输出:
false
使用子字符串
使用子字符串类似于使用String类的方法:
substring(String str, int start)
substring (String str, int start, int end)
这些方法从字符串str返回一个子字符串。该字符串由两个索引给出:start和end。和 Java 一样,范围的最后一个字符是end-1。这些方法的优点是什么?
如果将null传递给这样的方法,它只会返回null而不是抛出异常。这些方法支持负索引值。在这种情况下,字符串被认为是一个闭环。最后一个字符之后是第一个,依此类推。
让我们看看如何使用它:
System.out.println(StringUtils.substring("lets java", 2, 6));
System.out.println(StringUtils.substring("lets java", -8));
System.out.println(StringUtils.substring(null, 3));
运行上面的代码给我们:
ts j
ets java
null
StringUtils.split() 函数
一种允许您使用特殊分隔符将字符串拆分为子字符串的方法。如果目标字符串中有一个,则该方法将返回一个子字符串数组。如果没有字符,将返回一个空数组。好吧,如果将null传递给 method ,它将返回null。让我们看一下这段代码以及该方法的工作原理:
String myData = "Address, City, State, Zip, Phone, Email, Password";
System.out.println(Arrays.toString(StringUtils.split(myData, ',')));
System.out.println(Arrays.toString(StringUtils.split(null, '.')));
System.out.println(Arrays.toString(StringUtils.split("", '.')));
结果:
[Address, City, State, Zip, Phone, Email, Password]
null
[]
StringUtils.join()
join()方法允许您将字符串数组连接成一个字符串。同时,可以向其传递一个特殊的分隔符,在结果字符串的子字符串之间添加该分隔符。如果将null传递给该方法,则它将返回null。
此方法与split()方法完全相反。让我们看一下这个简单的例子:
String myData = "Address, City, State, Zip, Phone, Email, Password";
String[] myString = StringUtils.split(myData, ',');
System.out.println(StringUtils.join(myString, '-'));
运行上面的代码给我们:
Address- City- State- Zip- Phone- Email- Password
StringUtils.replace()
在字符串中搜索字符串,如果存在则找到它,并用新字符串替换所有出现的字符串。
此方法的声明如下所示:
public static String replace(final String text, final String searchString, final String replacement)
如果在文本中找不到搜索字符串,则什么也不会发生,文本将保持不变。按照相同的逻辑,如果文本为null,则此方法返回null。如果您要查找空字符串或用null替换子字符串,则该方法将返回原始字符串。
让我们试试这个方法:
String value = "CodeGym is the best";
System.out.println(StringUtils.replace(value, "best", "cool"));
结果:
CodeGym is the cool
GO TO FULL VERSION