Java String substring()

Method substring() is used for getting a substring of a particular String. There are two variants of this method

String substring(int beginIndex): Returns the substring starting from the specified index(beginIndex) till the end of the string. For e.g. "Chaitanya".substring(2) would return "aitanya". This method throws IndexOutOfBoundsException If the beginIndex is less than zero or greater than the length of String (beginIndex<0||> length of String).

String substring(int beginIndex, int endIndex): Returns the substring starting from the given index(beginIndex) till the specified index(endIndex). For e.g. "Chaitanya".substring(2,5) would return "ait". It throws IndexOutOfBoundsException If the beginIndex is less than zero OR beginIndex > endIndex OR endIndex is greater than the length of String.

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); }
Signature
public String substring(int startIndex) and public String substring(int startIndex, int endIndex)

If you don't specify endIndex, java substring() method will return all the characters from startIndex.

Parameters

startIndex : starting index is inclusive

endIndex : ending index is exclusive

Returns

specified string

Throws

StringIndexOutOfBoundsException if start index is negative value or end index is lower than starting index.

Example: substring()
public class Test { public static void main(String args[]) { String str= new String("Java String substring method is overloaded and has two variants"); System.out.println("Substring starting from index 20:"); System.out.println(str.substring(20)); System.out.println("Substring starting from index 20 and ending at 30:"); System.out.println(str.substring(20, 30)); } }


Output:
Substring starting from index 20: g method is overloaded and has two variants Substring starting from index 20 and ending at 30: g method i
Example: substring()
// Java code to demonstrate the // working of substring(int begIndex) public class Test { public static void main(String args[]) { // Initializing String String Str = new String("Welcome to cprogramcoding"); // using substring() to extract substring // returns cprogramcoding System.out.print("The extracted substring is : "); System.out.println(Str.substring(10)); } }


Output:
The extracted substring is : cprogramcoding

String substring(begIndex, endIndex):This method has two variants and returns a new 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 or up to endIndex – 1, if the second argument is given.

Syntax : public String substring(int begIndex, int endIndex) Parameters : beginIndex : the begin index, inclusive. endIndex : the end index, exclusive. Return Value : The specified substring.
// Java code to demonstrate the // working of substring(int begIndex, int endIndex) public class Test { public static void main(String args[]) { // Initializing String String Str = new String("Welcome to cprogramcoding"); // using substring() to extract substring // returns geeks System.out.print("The extracted substring is : "); System.out.println(Str.substring(10, 16)); } }


Output:
The extracted substring is : cprog

Possible application : The substring extraction finds its use in many application including prefix and suffix extraction. For example to extract a lastname from name or extract only denomination from a string containing both amount and currency symbol. The latter one is explained below.

/Java code to demonstrate the // application of substring() public class Test { public static void main(String args[]) { String Str = new String("Rs 5000"); System.out.print("The original string is : "); System.out.println(Str); System.out.print("The extracted substring is : "); System.out.println(Str.substring(3)); } }


Output:
The original string is : Rs 5000 The extracted substring is : 5000
Java String substring() method example
public class Test { public static void main(String args[]) { String str1="cprogramcoding"; System.out.println(str1.substring(3,7)); System.out.println(str1.substring(5)); } }


Output:
ogra ramcoding
Java String substring() Method Example
public class Test { public static void main(String[] args) { String s1="cprogramcoding"; String sub = s1.substring(0); System.out.println(sub); String substr2 = s1.substring(2,7); System.out.println(substr2); String substr3 = s1.substring(3,8); } }


Output:
cprogramcoding rogra



Instagram