Java string substring
In this article, we will learn how to find substring of a string in java using substring() method of string class with example programs.
Java string class has a
substring()
method which is used to find substring or a portion of a string between a some given indexes. Remember that characters in a java string have numeric indexes, with the first character having index 0, second having index 1 and so on.
Note that the index of last character is equal to the (length of string – 1).
String has two overloaded versions of substring()
method.
1. substring(int beginIndex)
This method accepts an integer argument and returns a substring of the original string with characters starting from the given index till the end of the string.
Java docs for this method state,
Returns a 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
Example,
String s = "tutifruity"; String sub = s.substring(4); System.out.println("Original String: " + s); System.out.println("Substring: " + sub);
Output is
Original String: tutifruity
Substring: fruity
Note that substring()
returns a new string. Original string remains unmodified.
This is because String is immutable.
This method will throw StringIndexOutOfBoundsException
in any of the below conditions,
A. beginIndex is negative.
B. beginIndex is greater than the length of the string.
This method accepts two integer arguments and returns a substring of original string with characters between those indexes.
Character at beginIndex is included in the result while character at endIndex is not included.
Thus, it returns characters from beginIndex till (endIndex – 1).
Java docs for this method state,
Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. Thus the length of the substring is endIndex-beginIndex.
Example program for this substring()
method is,
String s = "tutifruity"; String sub = s.substring(0,4); System.out.println("Original String: " + s); System.out.println("Substring: " + sub);
Output is
Original String: tutifruity
Substring: tuti
This method also returns a new string instead of modifying the original string.
This method will throw StringIndexOutOfBoundsException
in any of the below conditions,
A. beginIndex is negative.
B. beginIndex is greater than endIndex.
C. endIndex is greater than the length of the string.
Applications of substring()
1. substring(int)
method is extensively used in scenario where we need to get a string with some prefix removed.
So, we can directly supply the length of prefix and it will return a list after that index till the end of string. Example,
String s = "premainpost"; String sub = s.substring(3); System.out.println("Original String: " + s); System.out.println("Prefix removed: " + sub);
2. substring(int, int)
can be used to remove both prefix and suffix from a string by supplying the length of prefix as first argument and length of the string minus the length of suffix as second argument. Example,
String s = "premainpost"; String sub = s.substring(3, s.length() - 4); System.out.println("Original String: " + s); System.out.println("Prefix & suffix removed: " + sub);
Hope the article was useful.