Substring Method in Java

Introduction

 
In Java, the substring method is very useful. With the substring method we can take out a part of the text and print it. Suppose we need to write only four letters of the complete name then it is very useful for that purpose.
 

Substring ( )

 
When we need only a part of the complete text then we use the substring method. The substring method takes out the required text depending on the given instructions. The instructions can be provided to, for example, start from a specific position and then stop extracting the characters at a specific position of the string.
 

Various Examples of Substring method

 
Example 1
 
In this example, the substring method will take the text from the given string. It will start taking the characters from position one of the string and stop taking characters when three characters are taken.
  1. package demo99;  
  2. import java.util.*;  
  3. public class Demo99 {  
  4.  public static void main(String args[]) {  
  5.   String Name = "Raghu Ram Rathod";  
  6.   String NewName = "";  
  7.   NewName = Name.substring(13);  
  8.   System.out.println(NewName);  
  9.  }  
  10. }  
Output
 
substring
 
 
Example 2
 
In this example, the substring method will take the text from the given string. It will start taking the characters from position three of the string and stop at the end of the string.
package demo99;
  1. import java.util.*;  
  2. public class Demo99 {  
  3.  public static void main(String args[]) {  
  4.   String Name = "Raghu Ram Rathod";  
  5.   String NewName = "";  
  6.   NewName = Name.substring(3);  
  7.   System.out.println(NewName);  
  8.  }  
  9. }  
Output
 
substring(3)
 
 
Example 3
 
In this example the substring method will start taking the characters from position two of the string and go to the complete string but stops when three characters remain. Suppose we have a string "computer" then it will start taking out characters from position two of the string. The total length of the string is eight characters and eight minus three equals to five. So it will start taking out characters from position two and stop at position five of the string.
  1. package demo99;  
  2. import java.util.*;  
  3. public class Demo99 {  
  4.  public static void main(String args[]) {  
  5.   String Name = "computer";  
  6.   String NewName = "";  
  7.   NewName = Name.substring(2, Name.length() - 3);  
  8.   System.out.println(NewName);  
  9.  }  
  10. }  
Output
 
substring example 3
 
Example 4
 
This will provide you the position of the first occurrence of the space in the string. It will be an integer value.
  1. package demo99;  
  2. import java.util.*;  
  3. public class Demo99 {  
  4.  public static void main(String args[]) {  
  5.   String Name = "computer science";  
  6.   int PosOfSpace = Name.indexOf(" ");  
  7.   System.out.println("Position of space " + PosOfSpace);  
  8.  }  
  9. }   
Output
 
position of space
 
 
Example 5
 
In this example, the substring method will start taking out the characters from the position where it got its first space and one character further because it is spacePos+1. It will stop after taking out two characters from the starting position.
  1. package demo99;  
  2. import java.util.*;  
  3. public class Demo99 {  
  4.  public static void main(String args[]) {  
  5.   String Name = "Raghu Ram Rathod";  
  6.   String NewName = "";  
  7.   int PosOfSpace = Name.indexOf(" ");  
  8.   NewName = Name.substring(PosOfSpace + 4, (PosOfSpace + 4) + 4);  
  9.   System.out.println(NewName);  
  10.  }  
  11. }  
Output
 
substring with spaces
 
 

Summary

 
This article explained the substring method in Java.