Substring in Java

The substring() method, which returns a new string that is a substring of the original string, has two versions. The substring extends from the character at the given index to the last character of the string. The substring’s endindex starts at 1, not 0.

Java the built-in substring() function of the String class allows one to extract a substring from a given string using index values that are specified as arguments. StartIndex is inclusive and endIndex is exclusive for the substring() technique.

Syntax

public String substring(int begIndex)

Two methods are followed by a substring:

Public String substring(int startIndex) returns a new String object with the given string’s substring starting at startIndex (inclusive). When the startIndex is greater than the String’s length or lower than zero, the procedure throws an IndexOutOfBoundException.

Public String substring(int startIndex, int endIndex) creates a new String object using the given string’s substring from startIndex to endIndex. When the startIndex is less than zero, when the startIndex exceeds the endIndex, or when the endIndex exceeds the length of the String, the function raises an IndexOutOfBoundException.

In case of String:

  • startIndex: inclusive
  • endIndex: exclusive

Program:

public class TechVidvan {
    public static void main(String[] args) {
        String originalString = "Hello, World!";
        String substring1 = originalString.substring(0, 5);
        System.out.println("Substring 1: " + substring1);
        String substring2 = originalString.substring(7);
        System.out.println("Substring 2: " + substring2);
        char singleChar = originalString.charAt(4);
        System.out.println("Single Character at index 4: " + singleChar);
    }
}

Output:
Substring 1: Hello
Substring 2: World!
Single Character at index 4: o

Explanation:

Define the originalString variable, which contains the text “Hello, World!”.

Use the substring() method to extract a substring from index 0 (inclusive) to index 4 (exclusive), which extracts “Hello” from the originalString. This substring is stored in the substring1 variable and printed.

Use the substring() method to extract a substring starting at index 7 (inclusive) to the end of the string. This extracts “World!” from the originalString. This substring is stored in the substring2 variable and printed.

Use the charAt() method to retrieve the character at index 4 in the originalString, which is ‘o’. This character is stored in the singleChar variable and printed.

Using String.split() method:

import java.util.*;  
public class TechVidvan 
{    
public static void main(String args[])  
{    
String text= new String("Hello, My name is Sachin");  
String[] sentences = text.split("\\.");  
System.out.println(Arrays.toString(sentences));  
}  
}

Output:
[Hello, My name is Sachin]

Explanation:

We used the split() technique in the program above. It receives the parameter., which separates the string into another string after checking whether an is present in the phrase. It is kept in an array of sentences that are String objects.

String substring(begIndex, endIndex):

This method returns a new string that is a substring of this string and has two variations. The substring starts with the character located at the supplied index and continues up to endIndex – 1 if the second argument is provided.

Syntax:

public String substring(int begIndex, int endIndex);

Parameters:

  • beginIndex: the begin index, inclusive.
  • endIndex: the end index, exclusive.

Return Value:

  • The specified substring.
public class TechVidvan {
    public static void main(String args[])
    {
        String Str = new String("Welcome to TechVidvan");
        System.out.print("The extracted substring is : ");
        System.out.println(Str.substring(10, 16));
    }
}

Output:
The extracted substring is: TechV

Explanation:

O(n), where n is the length of the original string, is the time complexity. The substring is returned by the substring() method in constant time O(1).

Space Complexity: O(1), as the substring operation uses no additional storage.

Summary

In conclusion, Java’s substring method is an effective tool for obtaining substrings from existing strings. It gives you the ability to interact with individual text chunks within a longer string, which is helpful for a variety of string manipulation jobs.