Java StringUtils substring() Method

Java StringUtils substring() Method | In this blog, we study the substring method in the StringUtils class, this method returns the substring from the specified start and end index from the user. There are several variations in this method they are as follows:-

1) public static String substring(final String str, int start):- Gets a substring from the specified String avoiding exceptions.
2) public static String substring(final String str, int start, int end):- Gets a substring from the specified String avoiding exceptions.

3) public static String substringAfter(final String str, final int separator):- Gets the substring after the first occurrence of a separator. The separator is not returned.
4) public static String substringAfter(final String str, final String separator):- Gets the substring after the first occurrence of a separator. The separator is not returned.

5) public static String substringAfterLast(final String str, final int separator):- Gets the substring after the last occurrence of a separator. The separator is not returned.
6) public static String substringAfterLast(final String str, final String separator):- Gets the substring after the last occurrence of a separator. The separator is not returned.

7) public static String substringBefore(final String str, final int separator):- Gets the substring before the first occurrence of a separator. The separator is not returned.
8) public static String substringBefore(final String str, final String separator):- Gets the substring before the first occurrence of a separator. The separator is not returned.
9) public static String substringBeforeLast(final String str, final String separator):- Gets the substring before the last occurrence of a separator. The separator is not returned.

10) public static String substringBetween(final String str, final String tag):- Gets the String that is nested in between two instances of the same String.
11) public static String substringBetween(final String str, final String open, final String close):- Gets the String that is nested in between two Strings. Only the first match is returned.
12) public static String[ ] substringsBetween(final String str, final String open, final String close):- Searches a String for substrings delimited by a start and end tag, returning all matching substrings in an array.

Java StringUtils substring() Method Examples

Let us see an example of the public static String substring(final String str, int start) and public static String substring(final String str, int start, int end) methods.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String str = "The pen is mightier than the,,, sword...!";
        System.out.println("The substring is:" + StringUtils.substring(str, 7));
        System.out.println("The substring is:" + StringUtils.substring(str, 7, 20));
    }
}

Output:-

The substring is: is mightier than the,,, sword…!
The substring is: is mightier

Let us see an example of the public static String substringAfter(final String str, final int separator) and public static String substringAfter(final String str, final String separator) methods.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String str = "The pen is mightier than the,,, sword...!";
        System.out.println("The substring is:" + StringUtils.substringAfter(str, 7));
        System.out.println("The substring is:" + StringUtils.substringAfter(str, "."));
    }
}

Output:-

The substring is:
The substring is:..!

Let us see an example of the public static String substringAfterLast(final String str, final int separator) and public static String substringAfterLast(final String str, final String separator) methods.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String str = "The pen is mightier than the,,, sword...!";
        System.out.println("The substring is:" + StringUtils.substringAfterLast(str, 9));
        System.out.println("The substring is:" + StringUtils.substringAfterLast(str, ","));
    }
}

Output:-

The substring is:
The substring is: sword…!

Let us see an example of the public static String substringBefore(final String str, final int separator), public static String substringBefore(final String str, final String separator), and public static String substringBeforeLast(final String str, final String separator) methods.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String str = "The pen is mightier than the,,, sword...!";
        System.out.println("The substring is: " + StringUtils.substringBefore(str, 4));
        System.out.println("The substring is: " + StringUtils.substringBefore(str, ","));
        System.out.println("The substring is: " + StringUtils.substringBeforeLast(str, ","));
    }
}

Output:-

The substring is: The pen is mightier than the,,, sword…!
The substring is: The pen is mightier than the
The substring is: The pen is mightier than the,,

Let us see an example of the public static String substringBetween(final String str, final String tag), public static String substringBetween(final String str, final String open, final String close), and public static String[ ] substringsBetween(final String str, final String open, final String close) methods.

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String str = "The pen is mightier than the,,, sword...!";
        System.out.println("The substring is: " 
                + StringUtils.substringBetween(str, ","));
        System.out.println("The substring is: " 
                + StringUtils.substringBetween(str, "5", "10"));
        System.out.println("The substring is: " 
                + StringUtils.substringBetween(str, "m", "d"));
        System.out.println("The substring is: " 
                + Arrays.toString(StringUtils.substringsBetween(str, "m", "d")));
    }
}

Output:-

The substring is:
The substring is: null
The substring is: ightier than the,,, swor
The substring is: [ightier than the,,, swor]

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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