StringUtils 簡介

StringUtils是最常用的 Apache Commons 類。它包含各種實用程序和方法,可幫助開發人員避免編寫樣板文件或簡單地為基本操作編寫笨拙的代碼。

StringUtils類中的許多方法都有它們的java.lang.String等效方法,但與java.lang.String方法不同,它們是空值安全的。這意味著不會在最意外的時刻拋出NullPointerException 。

Apache Commons 包含許多方法,我們將介紹一些最常用的方法。

StringUtils 方法列表:

是空的() 檢查字符串是否為空
等於() 比較字符串
比較() 比較字符串
指數() 在字符串中查找子字符串
最后索引() 在字符串中查找子字符串
包含() 檢查子字符串是否在字符串中
containsIgnoreCase() 檢查字符串中是否出現子字符串,忽略大小寫
containsAny() 檢查子字符串是否出現在字符串中的任何位置
containsNone() 檢查子字符串是否出現在字符串中的任何位置
僅包含() 檢查子字符串是否在字符串中
子串() 獲取子串
分裂() 將字符串拆分為子字符串
加入() 連接子串
消除() 刪除子字符串
代替() 替換子串
計數匹配() 計算匹配次數

StringUtils.isEmpty() 和 StringUtils.isBlank()

這兩種方法都用於檢查字符串是否包含任何文本。如果字符串真的是空的,它們返回真。此外,如果字符串僅包含空格,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

同樣,在null引用上調用方法會導致NullPointerException,您需要在使用之前檢查引用變量是否為null 。

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()方法根據搜索序列是否包含在另一個序列中返回truefalse 。

如果將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返回一個子字符串。該字符串由兩個索引給出:startend。和 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傳遞給方法,它將返回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