Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Substring in Java

Last updated by Dipen Dadhaniya on Sep 25, 2024 at 11:01 PM | Reading time: 6 minutes

The fast well prepared banner

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

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Substring in Java
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings

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

Last updated on: 
October 10, 2024
Author

Dipen Dadhaniya

Engineering Manager at Interview Kickstart

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

Register for our webinar

How to Nail your next Technical Interview

1
Enter details
2
Select webinar slot
First Name Required*
Last Name Required*
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

Substring in Java

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Our tried & tested strategy for cracking interviews
blue tick
How FAANG hiring process works
blue tick
The 4 areas you must prepare for
blue tick
How you can accelerate your learnings
Register for Webinar
entroll-image