Technical Musings !!

Kamlesh's learning and rendezvous with the technical world

String Utility Classes in Java

leave a comment »

Manipulating strings is a part and parcel of a programmer’s day to day life. There are many string related operations which we use quite often. Some of the string operations are provided by Java (classes like String, StringBuilder etc.) and some we devise ourselves. For example, as a good practice the first thing we do while working with string is to check it for null and emptiness as shown below:

if (myString != null || !"".equals(myString)) {
// work with myString
}

For the above and some other frequently used cases of string manipulation we can use the string utility classes already provided by the third party libraries. The string utility class available in apache commons project, org.apache.commons.lang3.StringUtils, is one of the widely used string utility classes. This class has many utility methods which take care of commonly used string operations and most of these are null safe.

For example we can use the isBlank, isNotBlank methods from the StringUtils class to validate the string for empty and null as shown below:

if(StringUtils.isNotBlank(myString)) {
//work with myString
}

The java documentation of the StringUtils.isBlank method says:

public static boolean isBlank(CharSequence cs)
Checks if a CharSequence is whitespace, empty ("") or null.

StringUtils.isBlank(null) = true
StringUtils.isBlank("") = true
StringUtils.isBlank(" ") = true
StringUtils.isBlank("bob") = false
StringUtils.isBlank(" bob ") = false

Based on requirements we can also consider using isEmpty and isNotEmpty.

You can check the API documentation of org.apache.commons.lang3.StringUtils and have a look on other utility methods it provides.

If you are already using Spring framework in your application, you can consider using the StringUtils class available in Spring Framework for the utility methods it provides. This class has been mainly used inside Spring Framework but as it is available, we can use this. Some of the methods I liked from this class are:

// Convert an array or a collection to a comma delimited string (say, for creating a csv file) and vice versa
public static java.lang.String arrayToCommaDelimitedString(java.lang.Object[] arr)
public static java.lang.String[] commaDelimitedListToStringArray(java.lang.String str)

public static java.lang.String collectionToCommaDelimitedString(java.util.Collection<?> coll)
public static java.util.Set<java.lang.String> commaDelimitedListToSet(java.lang.String str)

//Trim an array of strings
public static java.lang.String[] trimArrayElements(java.lang.String[] array)

For more information on Spring Framework’s StringUtil class, check it’s API documentation.

Enjoy Learning,
Kamlesh

About these ads

Written by Kamlesh

May 7, 2013 at 3:53 PM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 334 other followers

%d bloggers like this: