bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java String substring() Method

❮ String Methods

Example

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.

One of the following

public String substring(int
start , int
end )
public String substring(int
start )

Parameter Values

ParameterDescription
startRequired. The index of the character at which the substring starts.
endOptional. 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

Previous

Java String subSequence() Method

Next

Java String toCharArray() Method