Center a String within another String in Java

Using the StringUtils class in the Apache Commons Library we can center a String within a new created String by calling the center() method of the StringUtils class. The new String returned will be filled with blank characters on both sides of the centered value.

In the example below we have a String value with the initial length of 10. We call center with that value and specify the new String length to 20 which means it will put 5 blank characters on each side. Finally we print out the new String using a dash before and after to make it more visible, and also print out the length of the new String.

Output:

There are also two overloaded center() methods that also takes a padding character as parameter (either as a char or a String). In the below code we add that padding character as an asterisk.

and output from the above code:

Capitalize first letter in a String using StringUtils in Apache Commons Lang

This example shows how to captialize the first letter in a string using the StringUtils class of the Apache Commons Lang library.
The call to the capitalize() method capitalized the first letter only and leave the rest unchanged.

Output from the code above:

How to abbreviate a String in Java with Apache Commons StringUtils class

A little example on how to abbreviate a String using StringUtils class from Apache Commons Lang. The StringUtils class has two abbreviate methods.

The first takes two parameters, the string to be abbreviated and the max length of the string.

The second adds another parameter, an offset, to the parameter list.

When the string is abbreviated three dots are added to the end or beginning where the string actually is shortened. The code below shows how it’s done:

In the code above the first string is abbreviated at the end while the second is abbreviated both from the beginning and end using an offset where the abbreviation should start. The StringUtils class adds three dots where the abbreviation takes place.
Output from the code above will be:

Remove any whitespace from start and end of a String in Java

This example shows how to remove any whitespace from the beginning and end of a String. This can be done with standard Java using the trim() method of the String class, but we’ll also show how to do it with the StringUtils class of the Apache Commons Lang library, since it is null-safe which means that even if you pass in a null value to them it won’t throw a NullPointerException but rather just return null. The standard Java way of doing this is not null-safe and will throw a NullPointerException if the parameter to the method is null.

Using the StringUtils class it is actually possible to use two methods. The strip() method is the one that should be used since it targets whitespace. The trim() method removes any control character with a value <= 32, but that includes whitespace also. We add an exclamation mark at the end of the values to be sure there are now whitespaces left.

Output from the code above:

The strip() method of StringUtils also have an overloaded method that takes two arguments, the string to be stripped and another string containing the characters that should be stripped from start and end:

Output from the code above is this. Note that only the trailing 'c' is removed, not the center one:

Both trim() and strip() methods of the StringUtils class have a method that returns an empty string if the argument passed to it is null (instead of returning null). The difference is shown below:

Output:

Check if String is null, empty or blank with Apache Commons StringUtils class

This example shows how to use the Apache Commons Lang to check if a string is an empty string, null or contains only white spaces. This is more convenient than writing several conditions in an if statement.

There are actually two methods that check if the string is an empty string or if it’s null, isEmpty() and isBlank(). The second method also checks if the string contains only white spaces.

Here is an example with a string that is null:

and here is an example with a string that is empty:

and here is an example with a string that contains only white spaces: