Introduction
Sometimes you need to extract a portion of a string. The substring()
method in Java is your tool for exactly that! It allows you to create a new String consisting of characters from the original String, starting at a specified index and optionally ending before another specified index.
Syntax
public String substring(int beginIndex)
public String substring(int beginIndex,
int endIndex)
Parameters
Parameter | Description |
---|---|
beginIndex |
The index at which the substring begins (inclusive). |
endIndex |
The index at which the substring ends (exclusive). That is, the character at endIndex is not included in the substring. |
Return Value
The substring()
method returns a new String that is a substring of the original string.
Examples
Example 1: Extracting from a single index
This example demonstrates using substring()
with only the starting index. It extracts characters from the beginning of the string up to (but not including) the specified index.
String str = "Hello World";
String sub = str.substring(5); // Extract substring starting at index 5
System.out.println(sub);
World
Here, we start the substring from index 5 of "Hello World". Index 5 points to the character 'W'. The returned substring includes characters at indices 5, 6, 7, 8, 9, and 10 ('W', 'o', 'r', 'l', 'd').
Example 2: Extracting between two indexes
This example shows how to specify both a starting and ending index for the substring.
String str = "Hello World";
String sub = str.substring(0, 5); // Extract substring from index 0 up to (but not including) index 5
System.out.println(sub);
Hello
In this case, we're extracting a substring that starts at index 0 and ends just before index 5. This includes the characters at indices 0, 1, 2, 3, and 4 ('H', 'e', 'l', 'l', 'o'). The character at index 5 (' ') is *not* included.
Example 3: Handling Index Out of Bounds
It's important to be aware that if your indices are out of bounds, you will get a StringIndexOutOfBoundsException
. Let’s look at an example where we try to extract from an invalid index.
String str = "Hello";
// This line will throw StringIndexOutOfBoundsException because endIndex is out of bounds
//String sub = str.substring(0, 10);
//System.out.println(sub);
The above code would result in a runtime error. When using the two-argument version of substring()
, ensure that both beginIndex
is non-negative and less than the length of the string, and that endIndex
is greater or equal to beginIndex
and less than the length of the string.
Example 4: Substring with start index equals string length
What happens when we specify a begin index equal to the string's length?
String str = "Hello";
String sub = str.substring(5); //beginIndex is equal to string length
System.out.println(sub);
When the begin index equals the string's length, an empty string is returned.
Example 5: Substring with start and end indexes equal
Let’s see what happens when we set both indexes to be equal in value
String str = "Hello";
String sub = str.substring(2, 2); //start index equals end index
System.out.println(sub);
When the start and end indexes are equal an empty string is returned.