This example takes two arguments the start index and end index and returns the characters between these two indexes. It returns “Pro” as ‘P’ starts from index 7 and ‘o’ is at index 9. Hence the output is “Programming”.Įxample-2: String string = "I Love Programming" We have mentioned only one index which is beginIndex, therefore, the substring() method returns all the characters from the given index to the end of the string. Java Substring ExampleĮxample-1: String string = "I Love Programming" Note that the substring() method receives two parameters having beginIndex as inclusive and endIndex is exclusive.
Throws:- IndexOutOfBoundsException, if beginIndex is negative or endIndex is larger than the length of given String object or beginIndex is larger than the endIndex.Parameter:- beginIndex, the beginning index, inclusive.Throws:- IndexOutOfBoundsException, if beginIndex is negative or larger than the length of given String object.Ģ) public String substring(int beginIndex, int endIndex).Return Value:- the specified substring.There is a total of 2 substring() methods in the Java string class including its overloaded forms.ġ) public String substring(int beginIndex) Java provides a substring() method in the String class to extract some part of the string. The second line contains two space-separated integers denoting the respective values of. Input Format The first line contains a single string denoting s. You'll find the String class' substring method helpful in completing this challenge. Substring Method In Java | A substring is a subset of a string. Java Substring Given a string, s, and two indices, start and end, print a substring consisting of all characters in the inclusive range from start to end-1.