Home » java » Java String substring() method

Java String substring() method

This method takes a int argument to generate sub string of string. It returns sub string starting from beginIndex value to last of string.
The second overloaded method allows to specify the ending index value also.
beginIndex parameters is inclusive and endIndex is exclusive.

Signature

public String substring(int beginIndex)
public String substring(int beginIndex,int endIndex)

Parameters

beginIndex –generate substring from this index value(inclusive).
endIndex-generate substring up to endIndex-1 value(exclusive).

Return

A substring

Throw

IndexOutOfBoundsException: If beginIndex is negative or endIndex is greater then length of string or if beginIndex is greater then endIndex.

Example

Result

Please follow and like us:
error