Author
Dipen Dadhaniya
Engineering Manager at Interview Kickstart
For Software Engineers at any level, preparing for a tech interview requires intense, well-directed preparation! You must practice a diverse set of problems and ensure that you cover a wide variety of topics. In this article, we’re going to cover a topic about strings in Java, which will, in turn, help you crack several complex problems — substring in Java.
In this article, we will discuss:
A substring is a contiguous sequence of a string.
For example, consider string S = “faangâ€.
Substrings of S are: { “fâ€, “faâ€, “faaâ€, “faanâ€, “faangâ€, “aâ€, “aaâ€, “aanâ€, “aangâ€, “aâ€, “anâ€, “angâ€, “nâ€, “ngâ€, “g†}
Few strings of S which are not substring: { “fangâ€, “fgâ€, “abcâ€, “fngâ€, “ag†}
In this article, we will see how to find the substring of a string using the substring() method in Java.
We use the Java substring() method to get the substring from the starting index to the ending index of a given string. There are two variations of this method; we will discuss both of them.
This method accepts an integer beginIndex and gives us the substring starting from the beginning index to the last index of the string.
Here indexing is zero-based indexing.
This method works for String and StringBuilder classes in Java.
Syntax:
S.substring(beginIndex)
Here “S†is the given string and “beginIndex†is the starting index of our string.
Parameters:
beginIndex: an integer denoting the start index of the substring.
Return value:
It returns the substring from beginIndex to the last index of the string.
Examples:
S = “Faangâ€
S.substr(1) = aang
S.substr(2) = ang
Here’s the implementation of the approach discussed above:
public class substring {
public static void main(String args[]) {
String S = new String(“Faang”);
// Using substring(beginIndex) to extract substring in String
System.out.print(“The extracted substring starting from index 1 in String is : “);
System.out.println(S.substring(1));
StringBuilder Str = new StringBuilder(“Faang”);
// Using substring(beginIndex) to extract substring in String builder
System.out.print(“The extracted substring starting from index 1 in StringBuilder is : “);
System.out.println(Str.substring(1));
}
}
Output:
The extracted substring starting from index 1 in String is: aang
The extracted substring starting from index 1 in StringBuilder is: aang
We use this method to extract the substring from the begin index to end index – 1. This method works for String and StringBuilder classes in Java.
Syntax:
S.substring(beginIndex, endIndex)
Here “S†is given string and “beginIndex†is the starting index of our string, and endIndex is the ending index of our string. Here we exclude endIndex in our string.
Parameters:
beginIndex: an integer denoting the start index of a substring.
Return value:
It returns the substring from beginIndex to the last index of the string.
Examples:
S = “Faangâ€
S.substr(1, 3) = aa
S.substr(1, 4) = aan
Here’s the implementation of the approach mentioned above:
public class substring {
public static void main(String args[]) {
String S = new String(“Faang”);
// Using substring(beginIndex) to extract substring in String
System.out.print(“The extracted substring from index 1 to index 2 in String is : “);
System.out.println(S.substring(1, 3));
StringBuilder Str = new StringBuilder(“Faang”);
// Using substring(beginIndex) to extract substring in String builder
System.out.print(“The extracted substring from index 1 to index 2 in StringBuilder is : “);
System.out.println(Str.substring(1, 3));
}
}
Output:
The extracted substring from index 1 to index 2 in String is: aa
The extracted substring from index 1 to index 2 in StringBuilder is: aa
Question 1: What are the applications of substring() in Java?
We can use substring() in Java to extract any substring in a given string.
For example, we can use it in the following scenarios:
A. Extracting the last name from the full name. i.e., extracting “stark†from “tony starkâ€
B. Extracting hour format from the clock. i.e., extracting “08†from “08:12:56â€.
Question 2: Can the end index parameter be greater than the first index parameter in substring()?
No, the end index parameter cannot be greater than the first index parameter in substring(). If you try to do so, you will get the “Index Out of Bound†error.
Question 1: Can we extract any substring using the substring() function in Java?
Yes, you can extract any substring.
Question 2: Can the begin index parameter be negative in the substring() method in Java?
No, the begin index parameter cannot be negative in the substring() method in Java. If you try to do so, you will get the “Index Out of Bounds†error.
Question 3: Can the begin index be greater than the size of the string?
No, the begin index cannot be greater than the size of the string. If you try to do so, you will get the “Index Out of Bound†error.
Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!
If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview prep, we have trained thousands of Software Engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
—————
Article contributed by Pankaj Sharma
Time Zone:
Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Just drop your name and email so we can send your Power Patterns PDF straight to your inbox. No Spam!
By sharing your contact details, you agree to our privacy policy.
Time Zone: Asia/Dhaka
We’ve sent the Power Patterns PDF to your inbox — it should arrive in the next 30 seconds.
📩 Can’t find it? Check your promotions or spam folder — and mark us as safe so you don’t miss future insights.
We’re hosting a private session where FAANG insiders walk through how they actually use these Power Patterns to crack interviews — and what sets top performers apart.
🎯 If you liked the PDF, you’ll love what we’re sharing next.
Time Zone: