Java String substring()
Substring is a part of the String. Java String substring() method returns a new string object from a given String.
There are two ways you can use the Java substring() method
- String substring(begIndex)
- String substring(begIndex, endIndex)
substring(beginIndex)
This method will return a new String object from the specified startIndex (inclusive) and up to the last character of the String. Syntax
public String substring(int beginIndex)
- beginIndex – the beginning index, inclusive.
String str = "Java String Tutorial";
String newStr = str.substring(5);
The above code returns String Tutorial . That means the substring function extract the new string from 5th character (start index 5 ).
Full Source
class TestClass{
public static void main (String[] args){
String str = "Java String Tutorial";
String newStr = str.substring(5);
System.out.println(newStr);
}
}
Output
String Tutorial
substring(beginIndex,endIndex)
This method creates a new String object containing the contents of the existing String object from specified startIndex up to the specified endIndex . Also in this method, startIndex is inclusive but endIndex is exclusive , which means if you want to remove the last character then you need to give substring(0, string.length()-1). Syntax
substring(int beginIndex, int endIndex)
- beginIndex – the beginning index, inclusive.
- endIndex – the ending index, exclusive.
String str = "Java String Tutorial";
String newStr = str.substring(5,11);
The above code returns String , that means it extract the string from the 5th character to 11th character.
Full Source
class TestClass{
public static void main (String[] args){
String str = "Java String Tutorial";
String newStr = str.substring(5,11);
System.out.println(newStr);
}
}
Output
String
How to extract first 4 character from a string?
class TestClass{
public static void main (String[] args){
String str = "Java String Tutorial";
String newStr = str.substring(0,4);
System.out.println(newStr);
}
}
Output
Java
How to extract last 8 characters from a String?
class TestClass{
public static void main (String[] args){
String str = "Java String Tutorial";
String newStr = str.substring((str.length()-8),str.length());
System.out.println(newStr);
}
}
Output
Tutorial
How to print one char on each line?
class TestClass{
public static void main (String[] args){
String str = "Java String Tutorial";
for(int i =0;i<str.length();i++){
System.out.println(str.substring(i,i+1));
}
}
}
Output
J
a
v
a
S
t
r
i
n
g
T
u
t
o
r
i
a
l
Checking Palindrome using substring() Method

In the following Java program , checking if the first letter and the last letter of the string is same or not. If they are not the same, return false . If it is same, call the checkPal method again recursively passing the string again after the first and last letter removed.
public class Main
{
public static void main(String[] args) {
String str = "NEVER";
if(checkPal(str))
System.out.println(str + " is a palindrome");
else
System.out.println(str + " is not a palindrome");
}
public static boolean checkPal(String pStr)
{
if(pStr.length() == 0 pStr.length() == 1)
return true;
if (!pStr.substring(0, 1).equals(pStr.substring(pStr.length() - 1)))
return false;
else
return checkPal(pStr.substring(1, pStr.length() - 1));
}
}
java StringIndexOutOfBounds Exception when using String.substring()
When using the Java substring() method, a subset of the character sequence can be extracted from a string. The substring index must be any value from 0 to the length of a string. The java.lang.StringIndexOutOfBoundsException thrown by String methods to indicate that the beginIndex is negative , or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex. The StringIndexOutOfBoundsException is one of the unchecked exceptions in Java. example
public class Main
{
public static void main(String[] args) {
String str="abc";
System.out.println(str.length());
System.out.println(str.substring(4));
}
}
output
Length of the string: 3
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1931)
at Main.main(Main.java:14)
Here you can see the length of the string is "3" and we try to extract the 4th character from the string. So, the above code would throw StirngIndexOutOfBoundException as its trying to access index 4 which doesn't exits.

How to solve the StringIndexOutOfBoundsException
- Check the length of the string.
- Exception handling using try...catch.
Check the length of the string
You can check the length of the string using String.length() method before using subString() and proceed to access the characters of it accordingly.
public class Main
{
public static void main(String[] args) {
String str="abc";
if(str.length()>=4){
System.out.println(str.substring(4));
}else System.out.println("Can't extract substring");
}
}
Exception handling using try...catch
Using Java exception handling can be easily solved once you make use of the stack trace provided by the JVM. Once you detect the method that threw the StringIndexOutOfBoundsException , then you must examine the provided arguments are valid or not.
public class Main
{
public static void main(String[] args) {
String str="abc";
try{
System.out.println(str.substring(4));
}catch(StringIndexOutOfBoundsException e){
System.out.println("StringIndexOutOfBoundsException occured!!");
//handling exception here
}
}
}
Related Topics
- How to Get the Length of a String
- Java String charAt() Method
- String indexOf() method
- Java String replace()
- Java String contains()
- String Comparison in Java
- Java String concat() Method
- Java String split() method
- Convert String to int
- Java StringBuilder Class
- StringTokenizer in Java
- How to convert int to String in Java?