In this tutorial we continue  the exploration of how to use StringUtils class ' methods with an example

1)isEmpty(String)

package stringutilstest;

import org.apache.commons.lang3.StringUtils;

public class StringUtilsTest {

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

And the result will be true

This method is one of the most useful methods of StringUtils class and returns true if the given string is empty or null.For example:

2) leftPad(String str,int size)

This method pads with whitespaces the given string.Note that size argument defines the size of string with the whitespaces.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.leftPad(utilTest,30));
    }
}

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

3)overlay(String str,String overlay,int start,int end)

This method overlays part or the whole given string with 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.overlay(utilTest,"This is another quite long string",0,utilTest.length()));
    }
}

Another useful group of methods is the one that consists of methods that has to do with removing a part of string either from the beggining ,the end of the string etc

4) remove(String str,String remove)

This method removes the remove string from the given 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 filename.fileExtention";
        
        System.out.println(StringUtils.remove(utilTest,".fileExtention"));
    }
}

5) replaceChars(String str, char searchChar, char replaceChar)

This method relplace searchChar with replaceChar.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.replaceChars(utilTest,' ','_'));
    }
}

And the result will be:This_is_a_quite_long_string

This method come with several different variations such as replaceOnce method which replaces the occurance of a string in another string only once

6) reverse(String str)

This method simply reverses the given 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.reverse(utilTest));
    }
}

And the result will be:gnirts gnol etiuq a si sihT

7) split(String str)

This method splits the given string using whitespace as separator.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";
        String[] splitted=StringUtils.split(utilTest);
        for (int i = 0; i < splitted.length; i++) {
            String string = splitted[i];
            System.out.println(string);
        }
    }
}

And the result will be:

This
is
a
quite
long
string

8) splitByCharacterType(String str)

This method splits the given string by the type of the characters forming the string.splitCharacterType method forms tokkens with characters of the same type.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";
        String[] splitted=StringUtils.splitByCharacterType(utilTest);
        for (int i = 0; i < splitted.length; i++) {
            String string = splitted[i];
            System.out.println(string);
        }
    }
}

 And the result will be:

this
 
is
 
a
 
quite
 
long
 
string

In the above example the separator character type was whitespace but it could also be a special character,a number etc.

9) strip(String str)

This method removes whitespaces from the beginning or the end of a 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.strip(utilTest));
    }
}

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

10) uncapitalize(String str)

This method -as its name suggests - uncapitalizes the given 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.uncapitalize(utilTest));
    }
}

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