Java Substring
What is a substring in Java?
Here by the name " substring " itself, we can easily come to know it is a part of a string or a subset of a string. For example, if "JavaTpoint " is a string, then Java, T, etc... are substrings. It is a built-in method of string.
Before knowing about the substrings. Let's recall the strings in general.
String in Java
Strings can be divided into their individual characters. For example,
String name = "JavaTpoint";
Or
String name = new String("JavaTpoint");
It can be represented as,
J | a | v | a | T | p | o | i | n | t |
0 1 2 3 4 5 6 7 8 9
JavaTpoint is a string that is divided into individual characters, and we can access these characters through index numbers. Every character has an index associated with it. The indexing in the string starts from 0 and ends at (n-1), where n is the length of the string. So, if we want to look for "J" then we should look at 0, not 1. A string has many operations like length, concatenation, comparison, etc… And many methods like replace(), contain(), substring(), etc…
Let us understand string with a simple program,
StringExample.java
public class StringExample
{
public static void main(String args[])
{
String name = "JavaTpoint"; // creating a string without new keyword
String name1 = new String("JavaTpoint"); // creating a string with new keyword
System.out.println(name); // displaying the string
System.out.println(name1); // displaying the string
}
}
Output :

Now let us come to our main topic which is substring,
substring() method in Java
The method substring() returns a String that is part of the original String. So, if I have “ HelloProgrammers “ as a string then, Programmers can be one of the substrings.
In order to get a substring from a given String, Java provides us with two methods. The two methods are :
1. substring (int start index)
2. substring (int start index, int end index)
We can choose any one of the above methods to get the substring of a string. The important point to note here is start index is inclusive and the end index is exclusive.
For example,
Let us consider the string “ JavaTpoint “. If we want just Java to print, then we should give parameters to the substring method as substring(0,4).
Here the string in the substring is not capitalized. Most of the methods in Java are " Camel case " that is the second word is capitalized. But here in the substring method, all are in lowercase.
substring(int start index) :
This method in Java returns the substring from the given starting index till the end.
This method will throw an exception that is " IndexOutOfBound " when giving a start index larger than the length of the string.
Let us understand this method with the program.
StringExample1.java
public class StringExample1
{
public static void main(String args[])
{
String s = new String("JavaTpoint is my favourite website"); // declaring a string using // new keyword
String ss = s.substring(5); // using substring method
System.out.println(" The substring is " +ss); // displays the substring
}
}
Output :

Here we have given the start index as 5. Since p is at the 5th index the substring starts from p.
public class StringExample2
{
public static void main(String args[])
{
String s = new String("JavaTpoint is my favourite website"); // declaring a string using // new keyword
String ss = s.substring(40); // using substring method
// run time error start index is larger than the length of the string
System.out.println(" The substring is " +ss);
}
}
Output :

Here we have given the start index as 40, length of the string is 34. Since 40 is greater than 34 that is given the start index is greater than the length of the string we have got the " IndexOutOfBound " exception.
substring(int start index, int end index)
This method in Java is used to get the substring of the desired length. This method also throws the exception " IndexOutOfBound " if the start index is less than 0 or greater than the end index or the end index is greater than the string length.
Let us understand this method using an example program
StringExample3.java
public class StringExample3
{
public static void main(String args[])
{
String s = new String("JavaTpoint is my favourite website"); // declaring string using // new keyword
String ss = s.substring(0,20); // using substring method
System.out.println(" The string is " +s); // displaying entire string
System.out.println(" The substring is " +ss); // displaying substring from 0 to 19th //index
}
}
Output :

Here we wanted to get a substring of length 0 to 19. Since the end index is exclusive, we gave 20 as the end index.
StringExample4.java
public class StringExample4
{
public static void main(String args[])
{
String s = new String("JavaTpoint is my favourite website"); // declaring a string using // new keyword
String ss = s.substring(0,40); // using substring method
System.out.println(" The string is " +s); // displaying entire string
System.out.println(" The substring is " +ss); // runtime error
}
}
Output :

Here end index is greater than the length of the string, so we got a run time error.
So, this is all about substring () method.