package org.apache.commons.lang.StringUtils
阿新 • • 發佈:2019-01-03
StringUtils是apache commons lang庫(http://commons.apache.org/lang)旗下的一個工具類,提供了很多有用的處理字串的方法,本文不打算把所有的方法都介紹一遍,我會介紹一些精選的常用的給大家。
目前StringUtils有兩個版本可用,分別是較新的org.apache.commons.lang3.StringUtils和較老的org.apache.commons.lang.StringUtils,他們有比較大的區別,前者需要JAVA 5,我想這個應該是我們希望使用的。
1)public static boolean equals(CharSequence str1,CharSequence str2)
我們就先從最簡單的方法equals開始,和你想的一樣,他需要兩個字串引數,當相同的時候返回true,否則返回false。
但是java.lang.String已經有現成的比較完美的equals方法了,為何我們還需要一個第三方的實現呢?
這個問題很好,讓我們來看看下面這些程式碼,看看有何問題?
Java程式碼
- public void doStuffWithString(String stringParam) {
- if
- // do stuff }
- }
- public void doStuffWithString(String stringParam) {
- if(stringParam.equals(
- // do stuff }
- }
這個可能有NullPointerException出現,那麼有幾個辦法處理:
Java程式碼
- public void safeDoStuffWithString1(String stringParam) {
- if(stringParam != null &&
- stringParam.equals("MyStringValue")) {
- // do stuff
- }
- }
- public void safeDoStuffWithString2(String stringParm) {
- if("MyStringValue".equals(stringParam))
- {
- // do stuff
- }
- }
- public void safeDoStuffWithString1(String stringParam) {
- if(stringParam != null &&
- stringParam.equals("MyStringValue")) {
- // do stuff
- }
- }
- public void safeDoStuffWithString2(String stringParm) {
- if("MyStringValue".equals(stringParam))
- {
- // do stuff
- }
- }
我本人不喜歡上面的兩個方法,第一個看起來太臃腫,第二個看起來像錯誤的。這裡我們就可以用一些StringUtils類了,這個類提供的equals方法是空指標安全的,不用擔心傳遞給他的是什麼引數,他不會丟擲空指標異常,這樣寫:
Java程式碼
- public void safeDoStuffWithString3(String stringParam) {
- if(StringUtils.equals(stringParam,"MyStringValue))
- {
- // do stuff
- }
- }
- public void safeDoStuffWithString3(String stringParam) {
- if(StringUtils.equals(stringParam,"MyStringValue))
- {
- // do stuff
- }
- }
這個是我個人的喜好,但是這個確實看起來比較簡單易讀。前面的兩個方法雖然麼有什麼問題,但是我想StringUtils.equals還是值得考慮的。
2)isEmpty,isNotEmpty,isBlank,isNotBlank
和前面一樣,這些方法相對於jdk提供的isEmpty方法來說,多了一個“空指標安全”,即不用考慮傳遞引數的空值問題,讓我們來看一個例子:
Java程式碼
- if(myString != null && !myString.isEmpty()) {
- // 有點臃腫是把?
- // Do stuff with myString
- }
- if(StringUtils.isNotEmpty(myString)) { // 好多了吧
- // Do stuff with myString
- }
- if(myString != null && !myString.isEmpty()) {
- // 有點臃腫是把?
- // Do stuff with myString
- }
- if(StringUtils.isNotEmpty(myString)) { // 好多了吧
- // Do stuff with myString
- }
Blank和empty的區別
isBlank將在字串含有空白字元的時候,返回true,例如:
Java程式碼
- String someWhiteSpace = " \t \n";
- StringUtils.isEmpty(someWhiteSpace); // false
- StringUtils.isBlank(someWhiteSpace); // true
- String someWhiteSpace = " \t \n";
- StringUtils.isEmpty(someWhiteSpace); // false
- StringUtils.isBlank(someWhiteSpace); // true
3)public static String[] split(String str,String separatorChars)
當然,這個方法相對於String.split也是 空指標安全 的,當你嘗試 split一個null字串 的時候,將返回Null,一個Null的分隔符將按照空白字元分隔字串,但是,還有一個理由讓你可考慮使用StringUtils.split()方法,就是jdk自帶的String.split由於支援正則表示式進行分隔,所以可能帶來意想不到的後果,例如:
Java程式碼
- public void possiblyNotWhatYouWant() {
- String contrivedExampleString = "one.two.three.four";
- String[] result = contrivedExampleString.split(".");
- System.out.println(result.length); // 0
- }
- public void possiblyNotWhatYouWant() {
- String contrivedExampleString = "one.two.three.four";
- String[] result = contrivedExampleString.split(".");
- System.out.println(result.length); // 0
- }
上面很明顯你希望按照.分隔,但是jdk理解的“.”是正則表示式的任意字元,導致字串內任意字元都匹配,返回一個size=0的字串陣列。其實你只要傳遞"\\."就行了,但是這個確實是一個問題。
這樣,使用StringUtils.split就簡單多了,另外,我測試還發現StringUtils.split比jdk自帶的split要快四倍。
4)public static String join (Iterable iterable,String separator)
這個方法確實很實用 ,因為jdk自身沒有提供,簡單使用方法:
Java程式碼
- String[] numbers = {"one", "two", "three"};
- StringUtils.join(numbers,","); // returns "one,two,three"
- String[] numbers = {"one", "two", "three"};
- StringUtils.join(numbers,","); // returns "one,two,three"
當然你可以傳遞一個數字或者迭代序列iterators.
在JAVA中我們用的最多的類應該就是String了。對於String的處理說簡單也簡單,但是有的時候要自己去實現一些功能還是要浪費一點時間的。一年之前接觸了StringUtils這個工具類,就猛然愛上了它,日復一日心裡始終覺得這東西實在太好了。不敢獨享,所以決定要總