Java String substring() Method
Example
Return a substring from a string:
String myStr = "Hello, World!";
System.out.println(myStr.substring(7, 12));
Definition and Usage
The substring()
method returns a substring from the string.
If the end argument is not specified then the substring will end at the end of the string.
Syntax
One of the following:
public String substring(int start, int end)
public String substring(int start)
Parameter Values
Parameter | Description |
---|---|
start | Required. The index of the character at which the substring starts. |
end | Optional. The index after the last character of the substring. |
Technical Details
Returns: | A String containing a substring of the string. |
---|---|
Throws: | IndexOutOfBoundsException - If start or end are negative or they are greater than the length of the string or if start is greater than end. |
Java version: | Any |
❮ String Methods