Introduction to Apache Commons StringUtils

Source: Internet
Author: User
Tags iterable
Introduction to Apache Commons StringUtilsHot 4 already 1529 read 2012-05-06 21:27 tags: Apache apache commons stringutils Introduction

org.apache.commons.lang.StringUtils


StringUtils is a tool class in the Apache Commons Lang Library (Http://commons.apache.org/lang), which provides a number of useful ways to handle strings, and this article does not intend to introduce all the methods. I will introduce some of the selected and commonly used to you.


Currently there are two versions of StringUtils available, Respectively are newer org.apache.commons.lang3.StringUtils and older org.apache.commons.lang.StringUtils, they have a relatively large difference, the former need Java 5, I think this should be what we want to use.


1 public static Boolean equals (Charsequence str1,charsequence str2)

Let's start with the simplest method equals, as you would expect, he needs two string arguments, returns True when the same time, or false.

But Java.lang.String already has the perfect equals method, why we need a third party implementation.

This is a good question, so let's take a look at the code below to see what the problem is.

Java code public void dostuffwithstring (String stringparam) {if (Stringparam.equals ("Mystringvalue")) {//do stuff The source code copy print public void dostuffwithstring (String stringparam) {if (Stringparam.equals ("Mystringvalue")) {/ /Do Stuff}}

This may have nullpointerexception appear, then there are several ways to deal with:

Java code   PUBLIC VOID SAFEDOSTUFFWITHSTRING1 (string stringparam)  {     if ( stringparam != null &&     stringparam.equals ("MyStringValue"))  {    // do stuff    }    }      Public void safedostuffwithstring2 (string stringparm)  {     if ("Mystringvalue ". Equals (Stringparam))      {    // do stuff    }    }    Source code copy Print public void safedostuffwithstring1 (String stringparam)  {    if (stringparam != null &&    Stringparam.equals ("Mystringvalue"))  {   // do stuff   }    }     public void safedostuffwithstring2 (string stringparm)  {    if ("Mystringvalue". Equals (Stringparam))     {   // do  stuff   }   }   

I personally do not like the top two methods, the first one looks too bloated, the second looks like a mistake. Here we can use some StringUtils class, this class provides the Equals method is null pointer security, do not worry about passing to him what parameters, he will not throw null pointer exception, so write:

Java code public void SafeDoStuffWithString3 (String stringparam) {if (Stringutils.equals (Stringparam, Mystringvalue))    {//Do stuff}}    SOURCE copy print public void SafeDoStuffWithString3 (String stringparam) {if (Stringutils.equals (Stringparam, Mystringvalue)) {//Do stuff}}


This is my personal preference, but it does look easy to read. The previous two methods, although there is no problem, but I think Stringutils.equals is worth considering.

2) Isempty,isnotempty,isblank,isnotblank

As before, these methods have a "null pointer security" relative to the IsEmpty method provided by the JDK, that is, without considering the null value of passing arguments, let's look at an example:

Java code if (myString!= null &&!mystring.isempty ()) {//a bit bloated is put.     Do stuff with myString} if (Stringutils.isnotempty (myString)) {//much better//do stuff with myString   Source Code copy Print if (myString!= null &&!mystring.isempty ()) {//a bit bloated is put. Do stuff with myString} if (Stringutils.isnotempty (myString)) {//much better//do stuff with myString}

the difference between blank and empty

Isblank returns True when the string contains whitespace characters, for example:

Java code String somewhitespace = "\ \ n"; Stringutils.isempty (Somewhitespace); False Stringutils.isblank (somewhitespace);    True source code copy print String somewhitespace = "\ n"; Stringutils.isempty (Somewhitespace); False Stringutils.isblank (somewhitespace); True

3) public static string[] Split (String str,string separatorchars)
Of course This method is also safe relative to String.Split, and when you try to split a null string, you return null, and a null delimiter separates the string by a blank character, but there is another reason to consider using the Stringutils.split () method, which is JDK's string.split can have unintended consequences because it supports regular expressions, such as:
Java code public void Possiblynotwhatyouwant () {String contrivedexamplestring = "One.two.three.four";     String[] result = Contrivedexamplestring.split ("."); System.out.println (result.length);    0} Source code copy print public void Possiblynotwhatyouwant () {String contrivedexamplestring = "One.two.three.four";    String[] result = Contrivedexamplestring.split ("."); System.out.println (result.length); 0}
It's obvious that you want to follow the. Spacer, but the JDK understands "." Is any character of a regular expression that causes any character within the string to match, and returns an array of size=0 strings. In fact, you just have to pass "\." Yes, but this is really a problem.
In this way, the use of stringutils.split is much simpler, and I also found that stringutils.split is four times times faster than the JDK's own split.

4 public static String join (iterable iterable,string separator)
This approach is really practical because the JDK itself does not provide a simple way of using:

Java code string[] numbers = {"One", "two", "three"}; Stringutils.join (Numbers, ",");   Returns "One,two,three" source code copy print string[] numbers = {"One", "two", "three"}; Stringutils.join (Numbers, ","); Returns "One,two,three"

Of course you can pass a number or iteration sequence iterators.

Well, I'm sure that this library is really a more practical library, recommended for everyone to use.

API please refer to: http://commons.apache.org/lang/api-3.1/org/apache/commons/lang3/StringUtils.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.