Java Substring


Substring in Java is a subset of the main string specified by start and end indices. We can extract a substring using the substring() method. String class provides two forms of substring methods.

1. public String substring(int startIndex): This method returns a new string that is a substring of this string. Here, startIndex represents the index at which substring begins with a character. The substring starts at startIndex and executes until the end of the string. For example, 
  String s="India";
       s.substring(3); 
Here, startIndex is 3. So, it will return characters starting 3rd character till the end of s.

2. public String substring(int startIndex, int endIndex): This method returns a new string of all the characters from starting index up to ending index but not including, the ending index. For example,
    String s="India";
     s.substring(1,3); 
It will return the characters of s starting from 1st to 2nd positions.

Java Substring Example Programs


Let's see the example program related to this topic.
Program source code 1:
    package stringPrograms; public class SubstringTest { public static void main(String[] args) { String s1="HelloJava"; String s2=s1.substring(5); System.out.println(s2); String s3=s1.substring(3, 9); // Java System.out.println(s3); // loJava } }
    Output: Java loJava
1. s1.substring(3,9); will return the characters of s starting from 3rd to 8th positions and it will be stored in the heap area.
2. s1.substring(5); will return the characters starting from 5th character till the end of s1.
Let's see the memory concept in the below diagram.
Substring in Java
Key points: 
Index starts from 0 to (n-1).
Q. Consider the following program.
1. What will be the output of the following program?
2. How many string objects will be created in the heap and string constant pool?
    package stringPrograms; public class SubstringTest { public static void main(String[] args) { String s=new String("SachinTendulkar"); // Line (4) s.substring(5); // Line (5) System.out.println(s); String s2=s.substring(6, 15); // Line (7) System.out.println(s2); String s3=s2.substring(3); // (9) System.out.println(s3); } }
    Output: SachinTendulkar Tendulkar dulkar
2. Let's see the memory concept first.
Java Substring
Explanation of Memory concept: 
1. When JVM will execute Line 4, It will create an object in the heap area and stores the content "SachinTendulkar" in it. The reference variable 's' will be pointed to the object "SachinTendulkar" by JVM as shown in the figure. We know that for every string literal, JVM also creates one copy of the object in the string constant pool for future purpose and stores the content in it. 
2. When line 5 will be executed, substring() will return a new string "nTendulkar" starting from the 5th position till the end that is the substring of the original string. JVM will create a new object in the heap area and stores the content "nTendulkar". Since we are not pointing any reference variable to it. Therefore, the garbage collector will remove it from memory. Since 's' is still pointing to the "SachinTendulkar". Therefore, the output will be "SachinTendulakar".

3. During the execution of line 7, the substring method will create a new string of all the characters from 6th to 15th position of the main string and JVM will store the content "Tendulkar" by creating an object in the heap area. A reference variable 's2'  will point to this object by JVM and the output will be "Tendulkar".

4. Similarly, during the execution of line 9, an object with content "Tendulkar" will be called using the reference 's2' by JVM and substring() method will create a new string "dulkar" starting from 3rd position till the end. This new string will be stored in the heap area by creating a new object and 's3' will refer to this object. Therefore, the output is "dulkar".
As you can see in the above figure, a total of 5 objects have been created, one in the string constant pool and four in the heap area. So, the answer is 5.

Final words 
Hope that this tutorial will help you to understand the concept of Substring in Java with example programs and the explanation of memory concept.  I hope that you will have enjoyed this tutorial.
Thanks for reading!