Apache Commons Lang API comes with a very useful utility class for manipulating Strings.Although StringUtils class is being used for several years now still is very useful and in this tutorial will see some of its methods with an example:

1) Abbreviate(String str, int maxWidth) :

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.For example:

package stringutilstest;

import org.apache.commons.lang3.StringUtils;

public class StringUtilsTest {
    public static void main(String[] args) {
       String utilTest="This is a quite long string";
        System.out.println(StringUtils.abbreviate(utilTest,10));
    }
}

 And the result will be: This is...

Abbreviate method has an overloaded form that enables you to declare an offset.For example:

package stringutilstest;

import org.apache.commons.lang3.StringUtils;

public class StringUtilsTest {

    public static void main(String[] args) {
       String utilTest="This is a quite long string";
        System.out.println(StringUtils.abbreviate(utilTest,5, 10));
    }
}

 And the result will be :...is a...

2) Capitalize(String str)

All that this method does is to turn to upperCase the first character of the string.For example:

package stringutilstest;

import org.apache.commons.lang3.StringUtils;

public class StringUtilsTest {

    public static void main(String[] args) {
       String utilTest="this is a quite long string";
        System.out.println(StringUtils.capitalize(utilTest));
    }
}

 And the result will be: This is a quite long string

3) contains(CharSequence seq CharSequence searchSeq)

This method returns true if a string contains another string.For example:

package stringutilstest;

import org.apache.commons.lang3.StringUtils;

public class StringUtilsTest {

    public static void main(String[] args) {
       String utilTest="this is a quite long string";
        System.out.println(StringUtils.contains(utilTest,"is"));
    }
}

 And the result will be: true

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

Another intresting group of methods that consit fo methods that return a default string.

3) defaultIfBlank(String str,String defaultStr)

This method returns defaultStr argument if the given string is null or empty.For example:

package stringutilstest;

import org.apache.commons.lang3.StringUtils;

public class StringUtilsTest {

    public static void main(String[] args) {
       String utilTest=null;
        System.out.println(StringUtils.defaultIfBlank(utilTest,"The default string"));
    }
}

 And the result will be : The default string

4) equals(CharSequence cs1, CharSequence cs2)

This method compares the given sequences and is case sensitive.For example:

package stringutilstest;

import org.apache.commons.lang3.StringUtils;

public class StringUtilsTest {

    public static void main(String[] args) {
       String utilTest="This is a quite long string";
        System.out.println(StringUtils.equals(utilTest,"This is a quite long string"));
    }
}

And the result is true.On the other hand if the second string was "this is a quite long string" the result would be false

This method has also a form for case insesitive comparison of the givern strings named equalsIgnoreCase

Apart from the index and lastIndexOf group of methods,that are used for finding the index of a given string or character, another other interestring  group of methods is the one the one for checking if the given String is AlphaNumeric Empty or Null.The good thing with those methods is that they handle unicode characters and work well with none latin characters.

5) IsAlphaNumeric(String str)

This method returns true if the given string contains characters or digits.For example:

package stringutilstest;

import org.apache.commons.lang3.StringUtils;

public class StringUtilsTest {

    public static void main(String[] args) {
       String utilTest="Thisis1quitelongstring ";
        System.out.println(StringUtils.isAlphanumeric(utilTest));
    }
}

 And the result will be:true.