Java String substring() Function Example
A substring of a string is defined as a sequence of characters in the string starting at some index and going continuously (without skipping any index or changing the order of characters) up to some higher index. The right index of the substring must be greater than or equal to the left index.
Java String substring()
Java String substring() is an inbuilt method that returns the substring of the string starting from a specified index and going till the end of the string or up to a specified end index(excluding that index).
See the following figure.
Java substring() method always returns a new string, and the original string remains unchanged because String is immutable in Java.
Internal Implementation
public String substring(int beginIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); } int subLen = value.length - beginIndex; if (subLen < 0) { throw new StringIndexOutOfBoundsException(subLen); } return (beginIndex == 0) ? this : new String(value, beginIndex, subLen); }
Java String substring(int beginIndex)
Syntax: public String substring(int beginIndex)
Parameters:
int beginIndex – The index from which the substring is to begin
Returns: The substring beginning at the specified index and going till the end of the string.
Throws: Throws StringIndexOutOfBoundsException if the parameter is negative or exceeds the length of the string.
Example1.java: Returning substring till the end of the string.
public class Example1 { public static void main(String[] args) { String s1 = new String("Consider this example."); System.out.println(s1.substring(9)); } }
Output
Example1a.java: On passing a negative beginning index.
public class Example1a { public static void main(String[] args) { String s1 = new String("Consider this example."); System.out.println(s1.substring(-1)); } }
Output
Example1b.java: On passing a beginning index, which exceeds the length of the string.
public class Example1b { public static void main(String[] args) { String s1 = new String("example"); System.out.println(s1.substring(8)); } }
Output
Java String substring(int beginIndex, int endIndex) example
Syntax: public String substring(int beginIndex, int endIndex)
Parameters:
int beginIndex – The index from which the substring is to begin
int endIndex – The substring ends at endIndex – 1.
Returns: The substring beginning at the specified index and going till the index before the end index.
Throws: Throws StringIndexOutOfBoundsException if the parameter is negative or exceeds the length of the string.
Example2.java: Returning substring starting from beginIndex up to endIndex-1.
public class Example2 { public static void main(String[] args) { String s1 = new String("Consider this example."); System.out.println(s1.substring(9, 13)); } }
Output
Example3.java: On passing a negative index.
public class Example3 { public static void main(String[] args) { String s1 = new String("Consider this example."); System.out.println(s1.substring(9, -1)); } }
Output
Example4.java: On end index exceeding the length of a string.
public class Example4 { public static void main(String[] args) { String s1 = new String("Consider."); System.out.println(s1.substring(9, 10)); } }
Output
Conclusion
The Java String substring() method returns a new string that is a substring of this string. The substring() method is overloaded method and comes in two variants:
String substring(int beginIndex) String substring(int beginIndex, int endIndex)
Where method arguments are:
beginIndex – the beginning index, inclusive.
endIndex – the ending index, exclusive.
So, if you want to extract part of the string in Java, then you can use the Java substring() function.