String Substring Java

Substring in Java is a powerful feature that allows you to extract a portion of a string. It enables selecting a specific character section from a larger string based on a starting and ending index. This feature is incredibly useful when manipulating or analyzing specific parts of a string.

Using the substring functionality, you can easily extract the required information and perform various operations. Java provides a straightforward and flexible way to work with substrings, making it a valuable tool for string manipulation and processing tasks. Whether you need to extract a single word, a sentence, or even a specific segment of text, substring in Java can help you achieve that easily and efficiently.

Post On:String Substring Method in Java
Post Type:Java Tutorials
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:SoftwareTestingo Telegram Group

In this article, we are going to discuss:

  • What is a substring?
  • Different methods under substring
  • substring(int beginIndex)
  • substring(int beginIndex, int endIndex)
  • Substring program

Let’s start our discussion with a discussion of the questions:

What is a substring in Java?

A substring is a part of a string, which means it is a smaller piece of a bigger string. In Java, the substring() method extracts a substring from a string. Java frequently uses this method to create smaller strings from larger ones.

It’s important to note that since strings cannot be changed in Java (they are immutable), the original string remains the same, and the substring() method returns a new string that represents the extracted substring.

Example String: “SoftwareTestingo is a dedicated blog for learners”

The word “Blog” is a substring of the above string because it is a continuous sequence of characters. However, the word “STestingo” is not a substring because it is not continuous in the given string.

So, you can say that a substring is a subset of the string.

Methods Of substring()

There are two methods of substring() methods available. In this guide, we will see how to use this method with the help of examples.

  • String substring(int begIndex)
  • String substring​(int beginIndex, int endIndex)

String substring(): This method takes a string’s starting index and returns a new substring that begins at that startIndex. The range of the substring goes from the startIndex (including) to the end of the string.

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

Program: Write a Java Program to Extract a SubString From a String Using Substring().
package com.softwaretestingo.string;
public class SubStringExample1 
{
	public static void main(String[] args) 
	{
		// Initializing String
		String Str = new String("Welcome to SoftwareTestingo");

		System.out.println("The Substring is: "+Str.substring(11));
	}
}

Output:

The Substring is: SoftwareTestingo

String substring(begIndex, endIndex): The substring method can also take two parameters for the starting and ending indexes. This will return a new string that is a substring of the original string, starting from the startIndex and ending at endIndex – 1.

Program: Write a Java Program to Extract a SubString From a String Using Substring().
package com.softwaretestingo.string;
public class SubStringExample2 
{
	public static void main(String[] args) 
	{
		// Initializing String
		String Str = new String("Welcome to SoftwareTestingo");

		System.out.println("The Substring is: "+Str.substring(0,7));
	}
}

Output:

The Substring is: Welcome
Interview Program: Write a Java program to print all substrings of a given string.
package com.softwaretestingo.string;
public class InterviewStringProgram1 
{
	// 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:

A
B
C
AB
BC
ABC

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

Interview Program: Write a Java program to print all substrings of a given string using substring().
package com.softwaretestingo.string;
public class InterviewStringProgram2 
{
	// 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 = "xyz";
		SubString(str, str.length());
	}
}

Output:

x
xy
xyz
y
yz
z

Conclusions:

As we end this article on Substring in Java, we hope you have understood the topic clearly through the information shared in this tutorial. If you have any remaining questions or need further clarification, please feel free to ask. We strive to provide comprehensive and informative content, and your feedback is valuable in improving our tutorials. Thank you for reading, and we hope this article has benefited you.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment