In this tutorial, you will learn about String substring() method, its syntax, and usage with examples.

Java String substring() method

In Java, String substring() method is used to find the substring of the string, where the substring is defined by a specific starting index and an optional ending index in the string.

Syntax of substring()

There are two substring() methods defined in String class. Based on the number of arguments, respective method is called.

</>
Copy
public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)

Let us summarise these two definitions, and specify a single syntax to call String substring() method.

</>
Copy
string.substring(value, fromIndex)

where

ParameterDescription
beginIndex[Mandatory]
An int value.
The beginning index of the substring in the string.
endIndex[Optional]
An int value.
The ending index of the substring in the string. The character at the end index is not inclusive in the resulting substring.
If endIndex is not specified, then the substring spans till the end of the string.

substring() returns value of String type.

If the given start index or end index is out of bounds for the given string, of if begin index is greater than end index, then substring() method throws IndexOutOfBoundsException.

The following are some of the examples to call substring() on str string with valid arguments.

</>
Copy
// substring(int beginIndex)
str.substring(5)

// substring(int beginIndex, int endIndex) 
str.substring(5, 12)

Examples

1. substring() – Find substring of a string, where begin index is given, in Java

In this example, we are given a string value in str variable. We have to find the substring value whose start index is 5 in the string str.

Call substring() method on the string str, and pass the begin index as argument.

Java Program

</>
Copy
public class Main {
	public static void main(String[] args) {
		String str = "AppleBananaCherry";
		String substr = str.substring(5);
		System.out.println("Given string  : " + str);
		System.out.println("Substring     : " + substr);
	}
}

Output

Given string  : AppleBananaCherry
Substring     : BananaCherry

Explanation

A p p l e B a n a n a C h e r r y    ← given string
0 1 2 3 4 5 6 7 8...
          ↑
     start index
          B a n a n a C h e r r y    ← substring

2. substring() – Find substring of a string, where begin index and end index are given, in Java

In this example, we are given a string value in str variable. We have to find the substring value whose start index is 5 and end index is 12 in the string str.

Call substring() method on the string, and pass the begin index and end index as arguments in the respective order.

Java Program

</>
Copy
public class Main {
	public static void main(String[] args) {
		String str = "AppleBananaCherry";
		String substr = str.substring(5, 12);
		System.out.println("Given string  : " + str);
		System.out.println("Substring     : " + substr);
	}
}

Output

Given string  : AppleBananaCherry
Substring     : BananaC

Explanation

A p p l e B a n a n a  C  h  e  r  r  y    ← given string
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
          ↑               ↑
     start index        end index
          B a n a n a  C                   ← substring

Please note that the character at end index is not inclusive in the resulting substring.

3. substring() – When begin index of out of bounds for given string

In this example, we are given a string value in str variable of length 17. We have to find the substring value whose start index is 25, which is out of bounds for a given string of length 17.

When we call substring() method on the string, the method throws IndexOutOfBoundsException.

Java Program

</>
Copy
public class Main {
	public static void main(String[] args) {
		String str = "AppleBananaCherry";
		String substr = str.substring(25);
		System.out.println("Given string  : " + str);
		System.out.println("Substring     : " + substr);
	}
}

Output

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 25, end 17, length 17
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3734)
	at java.base/java.lang.String.substring(String.java:1903)
	at java.base/java.lang.String.substring(String.java:1876)
	at Main.main(Main.java:4)

Conclusion

In this Java String Methods tutorial, we have seen about String substring() method in Java, its syntax, and how to use String substring() method in Java programs with the help of examples.