Getting system information with the SystemUtils class from Apache Commons Lang

Apache Commons Lang has quite a neat class to be able to find out information about the system you’re currently working on.
It’s called SystemUtils and has both static methods and constants to get the information. You could get much of that information by fetching system properties, but then you need to know the name of these properties and it is not easy to remember all these.
It’s very straightforward to use the SystemUtils class as the following example shows. The code below shows just a small part of the information that the class can return, so just download the library and test for yourself or browse through the API documentation that can be found on their site (search for Apache Commons Lang).

Continue reading

Compare two String using StringUtils from Apache Commons

The StringUtils class of the Apache Commons Lang library supplies a method for comparing String without checking them for null values first.
Normally when comparing with standard Java methods of the String class you need to check them for null first to avoid any NullPointerException being thrown, like:

However, with the StringUtils class you don’t need to do any null checks since it takes care of that part for you. The only thing you need to provide are the two strings to compare:

The output from the code above is:

How to remove characters from a String in Java

This example shows how to easily remove characters from a String using the StringUtils class in the Apache Commons Lang library.
The StringUtils class in the library has two overloaded methods for remove().
The first takes a char as parameter specifying what character to remove. All characters of that type is removed in the returned String.
The second overloaded version takes a sequence of characters (a String) as parameter along with the value to replace in.
The code below demonstrates this:

The output from the code above is:

How to reverse a string in Java

This example shows how to reverse a String using the StringUtils class in the Apache Commons Lang library. It also shows how to reverse parts of a String that are delimited by a certain delimiter.

The StringUtils class has one method for reversing a String, reverse(), and one method for reversing with delimiter, reverseDelimited(). The first one simply takes the String to be reversed as parameter, and the second one takes the string to be reversed as first parameter and the delimiter as the second parameter of type char. The example below show how to use them:

First we reverse String ‘s’ and ‘s1’ without any delimiters. Then we reverse ‘s1’ with delimiter. The whole String gets reversed. Last we reverse ‘s2’ with delimiter. Here only 1 and 4 gets reversed. The output from the code above is:

Left pad and right pad a String in Java using Apache Commons

Sometimes you might need a fixed length string but the current value is much shorter so you need to fill out the string to a certain length. This is where the StringUtils class of the Apache Commons Lang library comes in handy with its methods leftPad() and rightPad().

There are three overloaded versions of each:

leftPad(String str, int size)
leftPad(String str, int size, char padChar)
leftPad(String str, int size, String padStr)

The first one takes the string to be padded and a size for the new padded string. The returned string will be padded on the left hand side with whitespaces. If the size is smaller than the original string, the original string will be returned.

The second one takes a padChar as a third parameter which simply means it will use that char instead of whitespaces for padding.

The third one takes a sequence of characters as third parameter and will use that whole string when padding. If the sum of padding characters is not divisible with the number of characters in the sequence the character sequence will simply be chopped at the right end.

There are corresponding methods for rightPad, which do exactly the same but on the right hand side of the string.

rightPad(String str, int size)
rightPad(String str, int size, char padChar)
rightPad(String str, int size, String padStr)

Below are a few examples of the leftPad methods:

The output from the above code is:

How to join strings in Java using the Apache Commons StringUtils class

This example shows how to join values together into a String using the Apache Commons Lang library. The StringUtils class comes with many overloaded versions of the join() method and the example below shows a few of them.

The first join call is made by passing in a list of Integers and a dash-separator to the join method.

The second and third call is made by passing in an iterator and a separator. First by sending in an iterator from the keyset of a map, and the second by passing in an iterator from the values of the same map.

The last call is made by passing in an array of Objects and a null separator. The null seperator will just join the values together without anything in between.

The output from the code above looks like this:

Count occurences of a String in another String in Java using Apache Commons

Here is a small example of how to count character sequences within a String using the Apache Commons StringUtils class.
The StringUtils class comes with a method countMatches() which takes two parameters, the String to analyze and the substring or sequence of characters to look for.
The method is case-sensitive so even if we have two occurences of the word ‘the’ below the method will only find one since the other is capitalized:

However, we can use the standard Java String-method toLowerCase if we want to ignore case:

Find common sequence (prefix) of characters in multiple strings in Java

This example shows how to find a common sequence of characters at the beginning of any number of Strings. The Apache Commons Lang library comes with a method called getCommonPrefix() to which you pass an array of Strings to be compared. The method returns the initial sequence of characters that is common to all of them. If no common sequence can be found, including if all values are null in the array, an empty String will be returned:

and here’s the output:

Delete whitespaces from a String in Java with Apache Commons StringUtils class

This example shows how to delete all whitespaces in a String using the Apache Commons StringUtils class which is very easy as it comes with a handy deleteWhitespace() method. It doesn’t matter where in the value the whitespaces are located, everyone will be removed. To the method you send the String that is to be stripped of whitespaces and from it you get the new String without whitespaces:

Below you can see the result from the code above:

Remove characters from the end of a String in Java using Apache Commons StringUtils class

This example shows how to use the StringUtils class of the Apache Commons Lang library to remove characters form the end of a String.
To accomplish that we use the method chomp() of the StringUtils class to which we send the String to be altered and a character sequence of the characters we want to be removed from the end of it.
The code below shows an example. The String s contains numbers separated by a comma and a whitespace. We want to get rid of them at the end of the String so we call chomp that returns a new String with the desired format:

The output from the code is:

There is an overloaded chomp method in the StringUtils class that doesn’t take a character sequence. That method removes any new line characters at the end of the String. New line characters include ‘\r’, ‘\n’ and ‘\r\n’ (carriage return and line feed).

The output from the above code is:

If you just want to remove the last character of a String, no matter what it is, use the chop() method instead:

Output: