×
☰ See All Chapters

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. 

startIndex: starts from index 0(inclusive).

endIndex: starts from index 1(exclusive).

substring-in-java-0
 

class StringHandling {

        public static void main(String args[]) {

                String s = "AdiTemp";

                System.out.println(s.substring(3));//

                System.out.println(s.substring(0, 0));//

                System.out.println(s.substring(0, 1));//

                System.out.println(s.substring(0, 3));//

        }

}

Output:

Temp

A

Adi


All Chapters
Author