apache commons StringUtils介紹

來源:互聯網
上載者:User
apache commons StringUtils介紹 熱4 已有 1529 次閱讀  2012-05-06 21:27  標籤:  apache  apache commons StringUtils介紹

org.apache.commons.lang.StringUtils


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(stringParam.equals("MyStringValue")) {     // do stuff }     }    原始碼 複製 列印 public void doStuffWithString(String stringParam) {    if(stringParam.equals("MyStringValue")) {    // 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方法是null 指標安全的,不用擔心傳遞給他的是什麼參數,他不會拋出null 指標異常,這樣寫:

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方法來說,多了一個“null 指標安全”,即不用考慮傳遞參數的空值問題,讓我們來看一個例子:

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也是null 指標安全的,當你嘗試split一個null字串的時候,將返回Null,一個Null的分隔字元將按照空白字元分隔字串,但是,還有一個理由讓你可考慮使用StringUtils.split()方法,就是jdk內建的String.split由於支援Regex進行分隔,所以可能帶來意想不到的後果,例如:
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理解的“.”是Regex的任一字元,導致字串內任一字元都匹配,返回一個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.

好了,我確信,這個庫確實是一個比較實用的庫,推薦大家使用。

API請參考: http://commons.apache.org/lang/api-3.1/org/apache/commons/lang3/StringUtils.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.