Introduction String Handling in Java

A part of string is called substring. In other name , substring is a subset of another string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex - 1 if second argument is given.

Substring from the given String object by one of the two methods:

  • public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive).
  • public String substring(int startIndex,int endIndex): This method returns new String object containing the substring of the given string from specified startIndex to endIndex.

Parameters

Here is the detail of parameters:

  • beginIndex -- the begin index, inclusive.
  • endIndex -- the end index, exclusive.

Return Value:

  • The specified substring.
String s="hello";  
System.out.println(s.substring(0,2));//he  

Example of java substring

 public class TestSubstring{  
 public static void main(String args[]){  
   String s="Mithilesh Singh";  
   System.out.println(s.substring(6));  
   System.out.println(s.substring(0,6)); 
 }  
}