Substring creates a new object out of source string by taking a portion of the original string. It is defined in defined in java.lang.String class.
substring() has two variants:-
substring() has two variants:-
- substring(int beginIndex): returns the substring starting from the specified index. The beginIndex is inclusive. This method throws IndexOutOfBoundsException if the beginIndex is less than zero or greater than the length of String.
- substring(int beginIndex, int endIndex): returns a new 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 (beginIndex is inclusive and endIndex is exclusive while getting the substring).
