Java String substring()


You can extract a substring using substring (). It has two forms. The first is

String substring(int startIndex)

Here, the startIndex indicates where the string will start. This form gives a copy of the string that starts at startIndex and ends at the end of the invite string.

The second form of the substring() allows you to specify both the beginning and end index of the string:

String substring(int startIndex, int endIdex)

Here, startIndex specifies the starting index and endIndex specifies the stopping point. The resulting string contains all the characters from the starting index but does not include the last index.


Example

Program
// Substring finding
public class Main 
{
  public static void main(String[] args) 
  {
   String org="I am a good boy";
   String find="good";
   System.out.println(org);
   System.out.println("org.substring(7,11) : "+org.substring(7,11));
  }
}