Substring in Java: Learn how to use substring() Method

Last updated on Jun 14,2021 13.8K Views

Substring in Java: Learn how to use substring() Method

edureka.co

Strings are basically defined as an array of characters. In Java, objects of a String are immutable which means that a constant cannot be changed once created. Finding the substring of a string is a very common question asked in the Java interviews. So, I’ll explain how Substring in Java works.

These are the topics that I’ll be covering in this article:

Let’s get started.

Substring in Java: What is a substring in Java? 

A part of a string is called a Substring. In other words, substring is a subset of another string. The Java substring() method returns a new string that is a substring of this string. substring() method is an overloaded method.

Syntax:
substring(//any one of the methods)
{
//body
}

Example:

This is a clear example of a substring in Java. Here, the string is “Edureka” but when you break it down into blocks, it’s composed of substrings which finally compute the string.

Let’s move ahead and understand the different methods under substring().

Substring in Java: Different methods under substring

There are basically two different methods under the substring() method. They are:

Let’s understand them in detail.

String substring(int begIndex)

This method has two variants and it returns a new string that is a substring of this string. The substring begins with a character at the specified index ‘beginIndex’ to the end of the string.

Syntax:
public String substring(int begIndex)

Note: the index starts from ‘0’ which refer to the first character of String.

Let’s take a look at an example.

public class Substring {
public static void main(String args[])
{
// Initializing String
String Str = new String("Welcome to Edureka");
// using substring() to extract substring
// returns Edureka
System.out.print("The substring is : ");
System.out.println(Str.substring(10));
}
}

Output: Edureka

String substring​(int beginIndex, int endIndex)

This method has two variants and it returns a new string which is a substring of this string. The substring begins with a character at the specified index  BeginIndex and extends to the end of the string or up to endIndex – 1 if another argument is present.

Syntax

public String substring(int begIndex, int endIndex)

Let’s work with an example.

public class Substring2 {
public static void main(String args[])
{
// Initializing String
String Str = new String("Welcome to Edureka");
// using substring() to extract substring
// returns edu
System.out.print("The substring  is : ");
System.out.println(Str.substring(10, 14));
}
}

Output: edu

Now, let’s move ahead to the final topic.

Substring in Java: Substring program

Let’s discuss a few programs that help in understanding Substring in Java.

Program to print all substrings of a given string
class Course{
// Function to print all sub strings
static void subString(char str[], int n)
{
// Pick starting point
for (int len = 1; len <= n; len++)
{
// Pick ending point
for (int i = 0; i <= n - len; i++)
{
//  Print characters from current starting point to current ending point.
int j = i + len - 1;
for (int k = i; k <= j; k++)
{
System.out.print(str[k]);
}
System.out.println();
}
}
}
// Driver program to test the above function
public static void main(String[] args)
{
char str[] = {'a', 'b', 'c'};
subString(str, str.length);
}
}

Output:

Next, using a Method (Using substr() function)

public class Substring {

// Function to print all substring
public static void SubString(String str, int n)
{
for (int i = 0; i < n; i++)
for (int j = i+1; j <= n; j++)
System.out.println(str.substring(i, j));
}

public static void main(String[] args)
{
String str = "abcd";
SubString(str, str.length());
}
}

Output:

This brings us to the end of this article where we have learned about Substring in Java. Hope you are clear with all that has been shared with you in this tutorial.

Make sure you practice as much as possible and revert your experience.  

If you found this article on “Substring in Java” relevant, check out the Edureka’s Java Course, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “Substring in Java” and our team will be glad to answer.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework