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
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
Example of String substring() Method
Java
public class Substr1 {
public static void main(String args[])
{
String Str = new String( "Welcome to geeksforgeeks" );
System.out.print( "The extracted substring is : " );
System.out.println(Str.substring( 10 ));
}
}
|
Output
The extracted substring is : geeksforgeeks
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
Example
Java
public class Substr2 {
public static void main(String args[])
{
String Str = new String( "Welcome to geeksforgeeks" );
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
public class Appli {
public static void main(String args[])
{
String Str = new String( "Rs 1000" );
System.out.print( "The original string is : " );
System.out.println(Str);
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Jun, 2023
Like Article
Save Article