Java StringUtils split() Method

Java StringUtils split() Method | In this blog, we will discuss the split() method in StringUtils class. The spit() method from StringUtils class takes two arguments one is a string which is needed to be split another one is the substring or a character which needs to be removed from the string. The split method returns the array of strings after the split is performed.

The overloaded forms of the StringUtils.split() are as follows:-

  • public static String[ ] split(final String str):- It splits the provided text into an array, using whitespace as the separator.
  • public static String[ ] split(final String str, final char separatorChar):- Splits the provided text into an array, separator specified.
  • public static String[ ] split(final String str, final String separatorChars):- Splits the provided text into an array, separator specified.
  • public static String[ ] split(final String str, final String separatorChars, final int max):- Splits the provided text into an array with a maximum length, separators specified.

Java StringUtils split() Method

Let us see an example for the Java StringUtils split() Method.

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "bababacca";
        String[] array = StringUtils.split(string, 'a');
        System.out.println(Arrays.toString(array));
    }
}

Output:-

[b, b, b, cc]

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "Know Program";
        System.out.println(Arrays.toString(StringUtils.split(string)));
        System.out.println(Arrays.toString(StringUtils.split(string, 'o')));
        System.out.println(Arrays.toString(StringUtils.split(string, "ro")));

    }
}

Output:-

[Know, Program]
[Kn, w Pr, gram]
[Kn, w P, g, am]

Notice that the StringUtils.split(string, “ro”) method splits a string by characters ‘r’, and ‘o’, and substring “ro”. But not only based on the substring “ro”.

Java Code To Split String With Specific Length Using StringUtils

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "Know Program";
        System.out.println(Arrays.toString(StringUtils.split(string, "ro")));
        System.out.println(Arrays.toString(StringUtils.split(string, "ro", 1)));
        System.out.println(Arrays.toString(StringUtils.split(string, "ro", 2)));
        System.out.println(Arrays.toString(StringUtils.split(string, "ro", 3)));
        System.out.println(Arrays.toString(StringUtils.split(string, "ro", 4)));
        System.out.println(Arrays.toString(StringUtils.split(string, "ro", 5)));
    }
}

Output:-

[Kn, w P, g, am]
[Know Program]
[Kn, w Program]
[Kn, w P, gram]
[Kn, w P, g, am]
[Kn, w P, g, am]

Java StringUtils split() And trim()

The split() and trim() are two different methods used to manipulate the string, even though both seem the same there are slight differences between them. The split() method removes the object we specify but the trim method removes only the whitespaces in the string. The split() method will return an array of strings, whereas the trim() method returns the original string without leading and trailing whitespaces.

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "       Know Program       ";
        System.out.println(Arrays.toString(StringUtils.split(string)));
        System.out.println(Arrays.toString(StringUtils.split(string, 'o')));
        System.out.println(StringUtils.trim(string));
    }
}

Output:-

[Know, Program]
[ Kn, w Pr, gram ]
Know Program

Similar Methods like StringUtils.split()

There are many similar methods like the split() method. They are as follows:-

  • public static String[ ] splitByCharacterType(final String str)
  • public static String[ ] splitByCharacterTypeCamelCase(final String str)
  • public static String[ ] splitByWholeSeparator(final String str, final String separator)
  • public static String[ ] splitByWholeSeparator( final String str, final String separator, final int max)
  • public static String[ ] splitByWholeSeparatorPreserveAllTokens(final String str, final String separator)
  • public static String[ ] splitByWholeSeparatorPreserveAllTokens(final String str, final String separator, final int max)
  • public static String[ ] splitPreserveAllTokens(final String str)
  • public static String[ ] splitPreserveAllTokens(final String str, final char separatorChar)
  • public static String[ ] splitPreserveAllTokens(final String str, final String separatorChars)
  • public static String[ ] splitPreserveAllTokens(final String str, final String separatorChars, final int max)
import java.util.Arrays;
import org.apache.commons.lang3.StringUtils;

public class Main {

    public static void main(String[] args) {
        String string = "Know Program";

        System.out.println(Arrays.toString(StringUtils.splitByWholeSeparator(string, "o")));
        System.out.println(Arrays.toString(
            StringUtils.splitByWholeSeparator(string, "o", 3)));
        System.out.println(Arrays.toString(
            StringUtils.splitByWholeSeparatorPreserveAllTokens(string, "o")));
        System.out.println(Arrays.toString(
            StringUtils.splitByWholeSeparatorPreserveAllTokens(string, "o", 3)));

        System.out.println(Arrays.toString(StringUtils.splitPreserveAllTokens(string)));
        System.out.println(Arrays.toString(
            StringUtils.splitPreserveAllTokens(string, 'o')));
        System.out.println(Arrays.toString(
            StringUtils.splitPreserveAllTokens(string, "o")));
        System.out.println(Arrays.toString(
            StringUtils.splitPreserveAllTokens(string, "o", 1)));
    }
}

Output:-

[Kn, w Pr, gram]
[Kn, w Pr, gram]
[Kn, w Pr, gram]
[Kn, w Pr, gram]
[Know, Program]
[Kn, w Pr, gram]
[Kn, w Pr, gram]
[Know Program]

Leave a Comment

Your email address will not be published. Required fields are marked *