Substring in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

A substring in Java is a contiguous sequence of characters within a string. A substring can consist of any number of characters, from a single character to the entire string.

There are two methods in Java for a substring of a specified string:

  • String substring(startIndex)
  • String substring(startIndex, endIndex)

The first method returns a substring that starts from the specified index and goes to the end of the string, while the second method returns a substring that starts from the specified start index and ends at the specified end index (exclusive).

Take an example of a string name : “Mr Yuvraj Singh”

In the given string, if you want to remove Mr from the name and use only Yuvraj Singh, then you have to find out the substring of the whole string whose index ranges from 3 to 14 if you consider the 0 based indexing of the string.

Substring in Java

String substring() Method

Two different methods of signature use substring(), which helps to get a substring in Java. The first method only takes the starting index of the string and returns a new string that is substring, which begins at that startIndex and ends at the end of the string, which means the range of substring is from startIndex till the end of the string.

Syntax:

startIndex: It defines the starting point of the substring in the specified string.

Return Value

The function returns the specified substring from startIndex till the end of the string. The return type is String.

Example:

Output:

Explanation:

In the above example, It prints “Scaler” as a substring, starting from index 11 and ending at the string's end.

String substring(begIndex, endIndex)

Another variant of the Substring in Java takes parameters that are the starting index and ending index and It returns a new string that is a substring of this string, which begins at that startIndex and ends at the endIndex – 1 if the second parameter is given in the function.

Note: The startIndex is included in the range, and endIndex is not included, and indexing of the string is 0 based.

Syntax:

  • startIndex: It defines the starting point of the substring in the specified string.
  • endIndex: It defines the ending point of the substring in the specified string. The substring ends at endIndex-1.

Return Value:

The function returns the specified substring from startIndex till the end of the string. The return type is String.

Note: If the start index is negative or the end index is lower than the start index, the method throws StringIndexOutOfBoundsException.

Example:

Output:

Explanation:

In the above example, It prints The second part, which will print “Welcome” as a substring where it starts from index 0 and ends at index 6.

String.split() Method

The split() method is used to split and extract the substrings based on a delimiter.

Syntax:

Return Value:

The split() method in java returns an array of strings.

Example:

Output:

Explanation: This code splits the sentence into words based on spaces but limits the number of splits to 3. So, only the first three words will be extracted and printed.

Conclusion

  • A substring in Java is a contiguous sequence of characters within a string.
  • Java provides two methods for extracting substrings from a string: String substring(startIndex) and String substring(startIndex, endIndex).
  • The substring(startIndex) method returns a substring that starts from the specified index and goes to the end of the string.
  • The substring(startIndex, endIndex) method returns a substring that starts from the specified start index and ends at the specified end index (exclusive).