In Java, String is an object that represents a sequence of characters! Sometimes, we are not interesed in all characters, we need just to manipulate some of them to solve one problem or another! This is where Substring comes to rescue!

A substring, as the name implies, is a contiguous sequence of characters within a string! Every subset or portion of an original string is called a Sub string.

In this article, I will try to explain how to find all substrings of a string in Java and how substring is internally implemented! I will try to take you through all the available substring methods, their usage and implementation!

Stay tuned!

Playing around substrings in Java

In this chapter, I’m going to show you how to get Substrings of a given String in Java backed with some examples to fully understand how each method is implemented!

For example, “dev” String is composed of the following substrings: “d”, “de”, “dev”, “e”, “ev” and “v”! Empty string is considered a substring of every Java string!

Playing around substrings in Java

Java String substring() methods

substring method belongs to java.lang.String class and it is mainly used to find substrings of a specified String object. There are basically two overloaded variants of substring() method :

  • public String substring(int startIndex) accepts one parameter!

  • public String substring(int startIndex, int endIndex) accepts two parameters!

For methods arguments:

  • beginIndex : describes the first index, it is inclusive for both methods.

  • endIndex : describes the last index, it is exclusive for both methods.

Let’s see how they are implemented!

Important key points about String substring() method

The following are some key points you should keep in mind about substring method :

  • substring() methods returns new string instance - because String is immutable in Java - that is a substring of a given string.

  • For a string of length n, there are (n*(n+1))/2 non empty substrings and an empty string.

  • Both methods can throw IndexOutOfBoundsException when beginIndex is negative, larger than endIndex or larger than length of String!

  • beginIndex is inclusive and endIndex is exclusive.

  • The index “0” refers to first character of Sting! A valid index must be a number between 0 and the length of the string!

substring(int beginIndex) method example

This method returns a portion of string that is a substring of the original string. The substring starts with the character at the specified ‘beginIndex’ index till the end of string.

substring method variant two

Let’s see how substring(int beginIndex) method is implemented in JDK 9! You can check this link for more information about String class!

    
    public String substring(int beginIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = length() - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        if (beginIndex == 0) {
            return this;
        }
        return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
                          : StringUTF16.newString(value, beginIndex, subLen);
    }
    

The following example shows how to use substring(int beginIndex) to get substrings of a string in Java:

    
        public class SubstringsOfString {
            public SubstringsOfString() { 
            }
            public static void  main(String[] args) {
                String mystr = System."devwithus.com";
                System.out.println(mystr.substring(2));
                System.out.println(mystr.substring(5));
                System.out.println(mystr.substring(7));
            }
        }
    

Program output:

vwithus.com
thus.com
us.com
    

substring(int beginIndex, int endIndex) method example

This overloaded substring() method accepts two parameters and produces a new string that is a substring of the given string. The substring stars with the character at the specified ‘beginIndex’ index (included) to the ‘endIndex’ (excluded) index.

substring method variant one

This variant of substring method is implemented in JDK9 as follow:

    
    public String substring(int beginIndex, int endIndex) {
        int length = length();
        checkBoundsBeginEnd(beginIndex, endIndex, length);
        int subLen = endIndex - beginIndex;
        if (beginIndex == 0 && endIndex == length) {
            return this;
        }
        return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
                          : StringUTF16.newString(value, beginIndex, subLen);
    }
    private static void checkBoundsBeginEnd(int begin, int end, int length) {
        if (begin < 0 || begin > end || end > length) {
            throw new StringIndexOutOfBoundsException(
                "begin " + begin + ", end " + end + ", length " + length);
        }
    }
    

Let’s create a Java program that creates substring of a string from the specified start and end indexes using substring(int beginIndex, int endIndex) method!

    
        public class SubstringsOfString2 {
            public SubstringsOfString2() { 
            }
            public static void  main(String[] args) {
                String mystr = "devwithus blog";
                System.out.println(mystr.substring(1,5));
                System.out.println(mystr.substring(5,5));
                System.out.println(mystr.substring(7,9));
            }
        }
    

Output of the above substring example program is:

vwithus.com
thus.com
us.com
    

Java program to find all substrings of a string:

Now, let’s see how to write a Java program that prints out all possible substrings of a given string using substring() method!

In the following example, we will use two loops to extract all the substrings out of a specified string instance!

From Java 7, substring() method has O(n) time complexity, so the total time complexity will be O(n^3) since there are two for loops!

    
        public class AllSubstringsOfString {
            public AllSubstringsOfString() { 
            }
            public static void  main(String[] args) {
                String mystr = "dev";
                for (int i = 0; i < mystr.length(); i++) {
                    for (int j = i + 1; j <= mystr.length(); j++) {
                        String substr = mystr.substring(i, j);
                        System.out.println(substr);
                    }               
                }
            }
        }
    

When you run the above program, you will get the following result:

    
d
de
dev
e
ev
v
    

How substring method is implemented internally in Java ?

The behavior of substring() method is not the same in Java 6 and Java 7! The way how it is internally implemented in Pre Java 7 is totally different from the way how it is defined in Post Java 7!

Every time you call substring() method, it will return a new String because String is immutable in Java.

How substring is implemented internally in Java

Before Java 7u6:

Well, a String is basically a char array that contains all the characters. In order to manage the char array, String internally defines two variables : offset and count!

  • offset: The first index used to store the string.

  • count : The number of characters present in the string.

    
        // private constructor
        String(int offset, int count, char value[]) {
            this.value = value;
            this.offset = offset;
            this.count = count;
        }
        // substring method
        public String substring(int beginIndex, int endIndex) {
            if (beginIndex < 0) {
                throw new StringIndexOutOfBoundsException(beginIndex);
            }
            if (endIndex > count) {
                throw new StringIndexOutOfBoundsException(endIndex);
            }
            if (beginIndex > endIndex) {
                throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
            }
            return ((beginIndex == 0) && (endIndex == count)) ? this 
                                     : new String(offset + beginIndex, endIndex - beginIndex, value);
        }
    

When you call substring() method on a specified String instance, new values are assigned to offset and count variables to create a new String object without changing the internal char array!

So, if the original string is too long and hold a large array of size 2GB for example, no matter how small a substring is, it will share the same 2GB char array which can lead to memory leak issue!

Since Java 7u6:

Java 7u6 provides another mechanism to optimize the logic of substring operation in order to prevent memory leak problem! So, instead of sharing the underlying char[], substring method creates a new copy!

offset and count fields are removed from java.lang.String class because they are no longer used anymore! You can check 010257.html for further information!

In order to create a new string object from an original string, substring internally invokes Arrays.copyOfRange - called in constructor - method to create a new char[] copy!

    
        public String(char value[], int offset, int count) {
            if (offset < 0) {
                throw new StringIndexOutOfBoundsException(offset);
            }
            if (count < 0) {
                throw new StringIndexOutOfBoundsException(count);
            }
            if (offset > value.length - count) {
                throw new StringIndexOutOfBoundsException(offset + count);
            }
            this.offset = 0;
            this.count = count;
            this.value = Arrays.copyOfRange(value, offset, offset+count);
        }
    

You have to be carefull when using substring method as it can lead to serious issues if it is not used with care!

How to find all unique substrings in a string ?

In this chapter, we will try to write a Java program that will accept a string instance as input and generate all the possible unique substrings!

The most basic idea is to use the HashSet implementation to filter all generated substrings because HashSet does not accept duplicate values!

    
        import java.util.HashSet;
        import java.util.Iterator;
        import java.util.Set;
        public class UniqSubstrings {
            public UniqSubstrings() {
            }
            public static void main(String[] args) {
                String str = "deev";
                Set<String> uniqsubs = new HashSet<String>(); 
                for (int i = 0; i <= str.length(); i++) { 
                    for (int j = i + 1; j <= str.length(); j++) { 
                        uniqsubs.add(str.substring(i, j)); 
                    } 
                } 
                Iterator<String> siterator = uniqsubs.iterator();
                while(siterator.hasNext()){
                    System.out.println(siterator.next());
                }
            }
        }
    

When you run the above program, you will get the following output:

    
ee
de
ev
d
dee
e
v
eev
deev
    

Extract substring from end of a Java String

Sometimes, we need to extract some characters from the end of a given string to meet a specific programming need! For example, we need to print only the last 4 characters to hide some sensitive data when we are dealing with customer confidential informations.

Let’s say you want to extract the last five digits out of a phone number or the last four digits of a social security number!

Here is how you can do this using substring method in Java.

    
        public class SSNstring {
            public SSNstring() {
            }
            public static void main(String[] args) {
                // Extract the last 4 characters
                String ssn  = "474-94-4099";
                String subst = "";     
                if (ssn.length() > 4) {
                    subst = ssn.substring(ssn.length() - 4);
                } 
                else {
                    subst = ssn;
                }
                System.out.println("Last 4 digits of your social security number is : "+subst);
            }
        }
    

If you run the above program, the output will be:

    
Last 4 digits of your social security number is : 4099
    

How to check if String contains another subString in Java?

How to check if a string is a substring of another string is one of the most frequently asked question in Java!

There are multiple methods to achieve this task in Java. You can use contains() and indexOf() methods!

You can also use startsWith() and endsWith() methods to check whetever a given string starts or ends with another one!

    
        public class ContainsSubstrings {
            public ContainsSubstrings() {
            }
            public static void main(String[] args) {
                String mystr = "Checking substrings of a string in Java";
                 // Using contains method
                System.out.println(mystr.contains("dev"));
                System.out.println(mystr.contains("Java"));
                
                 // Using indexOf method
                System.out.println(mystr.indexOf("PHP")); 
                System.out.println(mystr.indexOf("string"));
                System.out.println(mystr.indexOf("Banana"));
                
                 // Using startsWith method
                System.out.println(mystr.startsWith("Checking")); 
                System.out.println(mystr.startsWith("of")); 
                
                 // Using endsWith method
                System.out.println(mystr.endsWith("substrings"));
                System.out.println(mystr.endsWith("Java")); 
            }
        }
    

When you execute the above example, you will see the following result:

    
false
true
-1
12
-1
true
false
false
true
    

How to check if a string is Palindrome using substring() ?

substring() method can be used also to check whetever a string is a Palindrome or not! Palindrome means that the string reads the same backwards as forwards like : level, radar, madam …

    
        public class PalindromeString {
            public PalindromeString() {
            }
            private static boolean isPalindrome(String str) {
                if (str == null)
                    return false;
                if (str.length() <= 1) {
                    return true;
                }
                String start = str.substring(0, 1);
                String end   = str.substring(str.length() - 1);
                if (!start.equals(end))
                    return false;
                else
                    return isPalindrome(str.substring(1, str.length() - 1));
            }
            public static void main(String[] args) {
                System.out.println(PalindromeString.isPalindrome("madam"));
                System.out.println(PalindromeString.isPalindrome("devwithus"));
            }
        }
    

Conclusion:

That’s all folks. In this article, we have discussed everything related to substring method! We have seen how to find all substrings of a string in Java programming language!

If you have come this far, it means that you liked what you are reading! Hope I didn’t strung you along too much and you found this article useful!

Stay tuned and see you next time!