Java String substring example shows how to get a substring from String in Java. The example also shows how to find substring in a string and then get a substring.
How to get a substring from String in Java?
Java String class provides two versions of substring
method to get a substring from the String as given below.
1 |
public String substring(int startIndex) |
This method returns a new string containing the substring starting from the startIndex till the length of the String.
1 |
public String substring(int startIndex, int endIndex) |
This method returns a string containing the substring starting from the startIndex till endIndex-1. The length of the returned substring is always equal to the endIndex – startIndex.
Consider below given String object.
1 |
String str = "Java string substring Example"; |
If you want to extract “string substring example” from the string, you can use the substring
method like given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javacodeexamples.stringexamples; public class StringSubStringExample { public static void main(String[] args) { String str = "Java string substring Example"; str = str.substring(5); System.out.println(str); } } |
Output
1 |
string substring Example |
We specified only the start index parameter to the substring
method. So substring
method will return a string containing substring starting from index 5 till string length, which in our case is “Substring Example”.
What if you want to extract “Java” from the String object given above? You need to specify both the start and end index of the String. “Java” starts from index 0 and goes till index 3. Let’s try to extract it using substring
method.
1 2 3 4 5 |
String str = "Java Substring Example"; str = str.substring(0, 3); System.out.println(str); |
Output
1 |
Jav |
Well, the output is not what we had expected. Instead of getting “Java”, we got only “Jav”. That is because, when you use substring
method, the start index is inclusive (included in the substring) but the end index is exclusive (not included in the substring). So when we specified index 0 to index 3, substring started from index 0 but ended on index 2, thus giving us only “Jav”. Below given is the correct program.
1 2 3 4 5 |
String str = "Java Substring Example"; str = str.substring(0, 4); System.out.println(str); |
Output
1 |
Java |
How to find substring in String in Java?
How do I know from where to start getting the substring if the String content is not known? You need to find substring in string to get the start index and/or end index in that case. You can find substring in string using the indexOf
and lastIndexOf
methods.
Consider below given string.
1 |
String strFilePath = "/opt/Java/options.java"; |
How to extract file name from the path? You can search the last index of “/” and start taking a substring from there till the end of the string as given below.
1 2 3 4 5 |
String strFilePath = "/opt/Java/options.java"; String strFileName = strFilePath.substring( strFilePath.lastIndexOf("/") + 1 ); System.out.println(strFileName); |
Output
1 |
options.java |
We added 1 to the last index of “/” because we did not want to include “/” in the substring.
Getting StringIndexOutOfBoundsException while getting the substring?
substring
method throws StringIndexOutOfBoundsException
in below given cases.
1) If the start index is negative
2) If the start index is greater than the string’s length
3) If the end index is specified and it is greater than the string’s length
4) If the end index is specified and the start index is greater than the end index
For example,
1 2 3 4 5 |
String str = "/opt/Java/options.java"; String strFileName = str.substring(-1); System.out.println(strFileName); |
Output
1 2 3 |
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(Unknown Source) at com.javacodeexamples.stringexamples.StringSubStringExample.main(StringSubStringExample.java:22) |
You should always check the start and end index parameters to avoid getting the StringIndexOutOfBoundsException exception.
This example is a part of the String in Java tutorial.
Please let me know your views in the comments section below.