The IoT Academy Blog

All You Should Know About Substrings in Java

  • Written By  

  • Published on October 31st, 2022

Table of Contents [show]

 

Introduction

 

A contiguous series of characters in a string or a subset or portion of another string are both considered substrings. A substring of "Substring in Java," for instance, is "Substring."

 

What does a Java substring mean?

 

One often used java.lang method is a substring.

Using a larger string as a starting point, a string class can produce smaller strings. The initial string remains unchanged since strings in Java are immutable, and the method returns the updated string.

 

Syntax:

string.substring(int startIndex, int endIndex)

 

Note: In this case, the string is an object of the String class, and endIndex is exclusive while startIndex is inclusive.

 

The method throws an IndexOutofBoundsException if:

  • endIndex is less than startIndex
  • endIndex/StartIndex is greater than the string length
  • endIndex/StartIndex is negative

 

Java substring() method

 

Example: 

TestSubstring.java

“`

  1. public class TestSubstring{    
  2.  public static void main(String args[]){    
  3.  String s="SachinTendulkar";    
  4.  System.out.println("Original String: " + s);  
  5.  System.out.println("Substring starting from index 6: " +s.substring(6));//Tendulkar    
  6.  System.out.println("Substring starting from index 0 to 6: "+s.substring(0,6)); //Sachin  
  7.  }  
  8. }    

“`

Output:

Original String: Sachin Tendulkar

Substring starting from index 6: Tendulkar

Substring starting from index 0 to 6: Sachin

 

Our Learners Also Read: What are the differences between C, C++, and Java?

 

You can get a substring from the given String object by one of the Two Methods:

 

1. Public String Substring(int startIndex):

 

This method returns a new String object containing a substring of the given string from the specified startIndex (inclusive). The method throws an IndexOutOfBoundException when startIndex is greater than the length of the string or less than zero.

 

Example:

Code: public class TestSubstring { 

  public static void main(String args[]) 

  {

   // Initializing the String 

    String Str = new String("Substring in Java Tutorial"); 

    // using substring() to extract substring 

    // should return: in Java Tutorial

    System.out.print("The extracted substring is : "); 

    System.out.println(Str.substring(10)); 

  } }

 

2. Public String Substring(int startIndex, int endIndex):

 

The substring of the given string from the startIndex to endIndex is returned by this method as a new String object. When startIndex is less than zero, when startIndex exceeds endIndex, or when endIndex exceeds the length of the string, the function throws an IndexOutOfBoundException.

 

Example:

Code: public class TestSubstring { 

    public static void main(String args[]) 

    { 

   // Initializing the String 

        String Str = new String("Substring in Java Tutorial"); 

        // using substring() to extract substring 

        // should return Java

        System.out.print("The extracted substring is : "); 

        System.out.println(Str.substring(13, 17)); 

    } 

}

 

Use and Application of Substrings

 

1. The substring in Java method is used in many applications, including suffix and prefix extraction. For example, if you want to extract the last name from a given name or extract the digits after the decimal point from a string containing digits before and after the period.

 

2. Check palindrome- We can use a substring in Java to check whether a given string is a palindrome or not, i.e., whether it reads the same from both ends.

 

3. To get all substrings of a string.

 

Explain the variants of the substring() method.

 

The two variants of the substring() method are:-

 

  • String substring()
  • String substring(startIndex, endIndex)

 

 

Here's how Two variants of a substring can be used in a Java Method:

 

1. Getting the first m and last n characters of a string.

Code:

public class TestSubstring {

public static void main(String[] args) {

String str = "Substring in Java Tutorial";

System.out.println("Last 8 char String: " + str.substring(str.length() – 8));

System.out.println("First 9 char String: " + str.substring(0, 9));

System.out.println("Topic name: " + str.substring(13, 26));

}

}

 

2. It is interesting to note that the substring in the Java method will return an empty string if the length of the string is passed as a parameter or if the same index is given as startIndex and endIndex. Here is an example:

Code: public class TestSubstring { 

public static void main(String[] args) { 

String quote = "Substring in Java Tutorial";

String substr = quote.substring(quote.length()); System.out.println(substr.isEmpty()); 

//if begin = end 

String sub = quote.substring(3,3); 

System.out.println(sub.isEmpty());

}}

 

3. Deleting the first character: Strings in Java start at index 0, i.e., the first character is indexed at 0. If you need to delete the first character, use substring(1). This returns a substring without the starting character, equivalent to deleting the first character.

Code: public class TestSubstring { 

public static void main(String[] args) { 

String quote = "Substring in Java Tutorial";

String substr = quote.substring(1); 

System.out.println(substr); 

}}

 

 

Conclusion

 

There are many applications of the "substring" function, such as finding all substrings of a string, finding prefixes and suffixes of a string, checking if a string is a palindrome, and many more. The substring method in Java helps us realize all the above scenarios. Be careful with substring function parameters, as they should not be outside the scope of the string.

 

About The Author:

logo

Digital Marketing Course

₹ 9,999/-Included 18% GST

Buy Course
  • Overview of Digital Marketing
  • SEO Basic Concepts
  • SMM and PPC Basics
  • Content and Email Marketing
  • Website Design
  • Free Certification

₹ 29,999/-Included 18% GST

Buy Course
  • Fundamentals of Digital Marketing
  • Core SEO, SMM, and SMO
  • Google Ads and Meta Ads
  • ORM & Content Marketing
  • 3 Month Internship
  • Free Certification
Trusted By
client icon trust pilot
1whatsapp