StringUtils is part of Apache Commons Lang (, and as the name suggest it provides some nice utilities for dealing with Strings, going beyond what is offered in java.lang.String. It consists of over 50 static methods. The org.apache.commons.lang3.StringUtils requires Java 5.0 and is probably the version you’ll want to use.

StringUtils class operations on the String that are null safe. StringUtils contains only static methods, means whenever we use these methods no need to initialize the StringUtils object.

  • IsEmpty/IsBlank – checks if a String contains text
  • Trim/Strip – removes leading and trailing whitespace
  • Equals – compares two strings null-safe
  • startsWith – check if a String starts with a prefix null-safe
  • endsWith – check if a String ends with a suffix null-safe
  • IndexOf/LastIndexOf/Contains – null-safe index-of checks
  • IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut – index-of any of a set of Strings
  • ContainsOnly/ContainsNone/ContainsAny – does String contains only/none/any of these characters
  • Substring/Left/Right/Mid – null-safe substring extractions
  • SubstringBefore/SubstringAfter/SubstringBetween – substring extraction relative to other strings
  • Split/Join – splits a String into an array of substrings and vice versa
  • Remove/Delete – removes part of a String
  • Replace/Overlay – Searches a String and replaces one String with another
  • Chomp/Chop – removes the last part of a String
  • LeftPad/RightPad/Center/Repeat – pads a String
  • UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize – changes the case of a String
  • CountMatches – counts the number of occurrences of one String in another
  • IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable – checks the characters in a String
  • DefaultString – protects against a null input String
  • Reverse/ReverseDelimited – reverses a String
  • Abbreviate – abbreviates a string using ellipsis
  • Difference – compares Strings and reports on their differences
  • LevensteinDistance – the number of changes needed to change one String into another


isEmpty() and isBlank() these methods checks whether text or not. If the String is null it returns true in both methods. isBlank() methods returns true if the String just contains whitespace.

if (testString != null && !testString.isEmpty()) {
   // Do the operation

if (StringUtils.isNotEmpty(testString)) {
   // Do the operation
 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true
 StringUtils.isEmpty(" ")       = false
 StringUtils.isEmpty("abcd")     = false
 StringUtils.isEmpty("  abcd  ") = false

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

Note: isNotEmpty() and isNotBlank() work in reverse to isEmpty() and isBlank() methods.


Trim() and stip() these methods removes leading and trailing whitespaces to String objects.


 StringUtils.trim(null)          = null
 StringUtils.trim("")            = ""
 StringUtils.trim("     ")       = ""
 StringUtils.trim("    joe    ") = "joe"


The equals() it takes two Strings and returns true if they are identical, or false if they are not. The equals() method in String and StringUtils works same, the slight difference is StringUtils equals() method is null safe.

public void testStringEquals(String testString) {
      if (testString.equals("TestString")) {
         //Do the operation

When we write these, we may get NullPointerException, if the testString is “null”. You can handle the NullPointerException using the following way.

public void testStringEquals(String testString) {
      if (testString != null && testString.equals("TestString")) {
         //Do the operation

This is where StringUtils.equals comes in handy, it’s null safe. It doesn’t matter what you pass it, it will not throw NullPointer Exception , sSo you could rewrite the simple method as follows:

 public void testStringEquals(String testString) {
      if (StringUtils.equals(testString, "TestString")) {
         //Do the opernations


As its name suggests this method abbreviate strings with ellipses.One thing to remeber here is that maxWidth argument is the length of the abbreviated string with the ellipses. Abbreviate method has an overloaded form that enables you to declare an offset.

public static String abbreviate(String str, int offset, int maxWidth)

String utilTest="This is a long string";
System.out.println(StringUtils.abbreviate(utilTest,5, 10));

The result will be : a...


The contains() method returns true if the string contains another string.

String utilTest = "The is Testing";

System.out.println(StringUtils.contains(utilTest, "is"));

StringUtils class has several methods similar to contains method such as containsAny,containsNone,containsIgnoreCase,containsWhitespace etc all of which return a boolean value.


The split() method splits the provided string into an array, based on the separator specified. This is an alternative to using StringTokenizer. The default delimiter is space.

String[] abcd = StringUtils.split("a b c")  = ["a", "b", "c"]
StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]


The join() method joins the elements of the provided array into a single String containing the provided list of elements. The default sepator is nothing here.

public static String join(Object[] array, char separator)

String[] srray = {"a", "b", "c"};
String abcd = StringUtils.join(srray , null) = "abc"

The main advantage of the StringUtils class is the string null safe handler. It has methods functions as same as the String class.