All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Program To Find Substring In A String In Java

Last Updated : Mar 11, 2024

Program To Find Substring In A String In Java

In this article we will show you the solution of program to find substring in a string in java, the substring() method of the Java String class returns a portion of the string.

In other words, the beginIndex begins at 0 and the endIndex begins at 1.

Substring methods are available in Java string in two different types.

The following conditions cause a StringIndexOutOfBoundsException to be thrown.

  • When there is a negative number as the starting index
  • Compared to the initial index, the final index is lower.

The process of extracting information from a string is referred to as substringing. We can, for example, have a list of names that must be followed.

A substring is a segment of String. Substring is, in other words, a subset of another String.

The Java String class includes the built-in substring() method, which extracts a substring from a given string using the index values passed as arguments. computer," the substrings will be com, compu, ter, and so on.

Substring(int startIndex): public String Returns a new String object matching the startIndex specified in the call to this method.

An object containing the substring from startIndex to endIndex of the given string is returned by this method.

This can be accomplished by performing a nested loop traversal of the given string, followed by another loop checking for sub-strings beginning at each index.

BeginningIndex and endIndex are inclusive and exclusive numbers that are passed to Java's substring method.

As a result, beginning at zero and ending at one is what the beginIndex represents.

As the name implies, substringing is the process of extracting information from strings.

java.lang.Substring takes two parameters, BeginningIndex and EndIndex.

In other words, the beginIndex represents a sequence of numbers starting at zero and ending at one. In substringing, strings are extracted of their information.

Step By Step Guide On Program To Find Substring In A String In Java :-

class SubstringsOfStringMain
{
public static void main(String args[])
{
  String str="abbc";
  System.out.println("All substring of abbc are:");
   for (int i = 0; i < str.length(); i++) {
   for (int j = i+1; j <= str.length(); j++) {
         System.out.println(str.substring(i,j));
   }
  }
}
}
  1. First, we must create a class with a name.
  2. A function called public static void main is then called to start the program.
  3. In the next step, we will define a string named ABC.
  4. An integer value is then assigned to a substring.
  5. The program is then closed with the system.out.printf function.

Conclusion :-

java.lang.Substring takes two parameters, BeginningIndex and EndIndex.

In other words, the beginIndex represents a sequence of numbers starting at zero and ending at one. In the substring, strings are extracted from their information.

A substring is a segment of a String. A substring is, in other words, a subset of another String.

The Java String class includes the built-in substring() method, which extracts a substring from a given string using the index values passed as arguments.

If the string is "computer," the substrings will be com, compu, ter, and so on.

I hope this article on program to find substring in a string in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪