About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Substring in Java

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: 

  • What Is a Substring in Java?
  • Java Substring Method
  • How to Use Substring in Java
  • Substring in Java Program
  • FAANG Interview Questions on a Substring in Java
  • FAQs on Substring in Java

What Is a Substring In Java?

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.

Java Substring Method

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.

String substring(beginIndex)

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

substring(beginIndex, endIndex)

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

FAANG Interview Questions on Substrings in Java

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.

FAQs on Substrings in Java

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.

Are You Ready to Nail Your Next Coding Interview?

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!

Sign up now!

—————

Article contributed by Pankaj Sharma

Attend our Free Webinar on How to Nail Your Next Technical Interview

Recommended Posts

All Posts