home page

Substring in Java

substring in java

A part of string is called substring. In other words, substring is a subset of another string.

In case of substring startIndex starts from 0 and endIndex starts from 1 or startIndex is inclusive and endIndex is exclusive.

You can get substring from the given String object by one of the two methods:

  1. public String substring(int startIndex): This method returns new String object containing the substring of the given string from specified startIndex (inclusive).
  2. 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.

In case of string:
  • startIndex:starts from index 0(inclusive).
  • endIndex:starts from index 1(exclusive).

Example of java substring

Test it Now
Output:Tendulkar
       Sachin