Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaSubstring in Java

Substring in Java

In Java, Substring is a part of a String or can be said subset of the String. There are two variants of the substring() method. This article depicts all of them, as follows : 

  • public String substring(int startIndex)
  • public String substring(int startIndex, int endIndex)
Java substring

Java Substring

1. String substring()

The substring() method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. Endindex of the substring starts from 1 and not from 0.

Syntax

public String substring(int begIndex);

Parameters

  • begIndex: the begin index, inclusive.

Return Value

  • The specified substring.

Example of String substring() Method

Java




// Java code to demonstrate the
// working of substring(int begIndex)
public class Substr1 {
    public static void main(String args[])
    {
 
        // Initializing String
        String Str = new String("Welcome to neveropen");
 
        // using substring() to extract substring
        // returns (whiteSpace)neveropen
 
        System.out.print("The extracted substring is : ");
        System.out.println(Str.substring(10));
    }
}


Output

The extracted substring is :  neveropen

2. String substring(begIndex, endIndex)

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

Syntax

public String substring(int begIndex, int endIndex);

Parameters

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

Return Value

  • The specified substring.

Example

Java




// Java code to demonstrate the
// working of substring(int begIndex, int endIndex)
 
// Driver Class
public class Substr2 {
    // main function
    public static void main(String args[])
    {
        // Initializing String
        String Str = new String("Welcome to neveropen");
 
        // using substring() to extract substring
        // returns geeks
        System.out.print("The extracted substring  is : ");
        System.out.println(Str.substring(10, 16));
    }
}


Output

The extracted substring  is :  geeks

The complexity of the above method

Time Complexity: O(n), where n is the length of the original string. The substring() method takes constant time O(1) to return the substring.

Space Complexity: O(1), as no extra space is required to perform the substring operation.

Possible Application

The substring extraction finds its use in many applications including prefix and suffix extraction. For example to extract a Lastname from the name or extract only the denomination from a string containing both the amount and currency symbol. The latter one is explained below.  

Below is the implementation of the above application

Java




// Java code to demonstrate the
// application of substring()
 
// Driver Class
public class Appli {
    // main function
    public static void main(String args[])
    {
        // Initializing String
        String Str = new String("Rs 1000");
 
        // Printing original string
        System.out.print("The original string  is : ");
        System.out.println(Str);
 
        // using substring() to extract substring
        // returns 1000
        System.out.print("The extracted substring  is : ");
        System.out.println(Str.substring(3));
    }
}


Output

The original string  is : Rs 1000
The extracted substring  is : 1000
RELATED ARTICLES

Most Popular

Recent Comments