Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Substring in Java | Example Programs

Java Substring


Substring in Java is a subset of the main string specified by start and end indices. We can extract a substring using 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 characters of s starting from 1st to 2nd position.

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 characters of s starting from 3rd to 8th position and it will be stored in the heap area.
2. s1.substring(5); will return characters starting from 5th character till the end of s1.

Let’s see the memory concept in the below diagram.

Key point: 
Index starts from 0 to (n-1).

Q. Consider the following program.
1. What will be 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");
 s.substring(5);
System.out.println(s); 
String s2 = s.substring(6, 15); 
System.out.println(s2); 
String s3 = s2.substring(3); 
System.out.println(s3); 
 } 
}
Output: 
       SachinTendulkar 
       Tendulkar 
       dulkar

2. Look at the memory concept first.

Explanation of Memory concept: 
1. When JVM will execute Line 6, It will create an object in the heap area and store content “SachinTendulkar” in it. The reference variable ‘s’ will be pointed to that 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 purposes and store content in it. 

2. When line 7 will be executed, substring() will return a new string “nTendulkar” starting from 5th position till the end that is the substring of the original string.

JVM will create a new object in the heap area and store content “nTendulkar” in it. 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 “SachinTendulkar”. Therefore, the output will be “SachinTendulakar”.

3. During the execution of line 9, the substring method will create a new string of all the characters from 6th to 15th position of the main string.

JVM will store 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 11, 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 has covered all important points related to Substring in Java with example programs. I hope that you will have understood memory concept with diagrams and enjoyed it.
Thanks for reading!!!
Next ⇒ String Constructor in Java⇐ PrevNext ⇒

The post Substring in Java | Example Programs appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Substring in Java | Example Programs

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×