String substring example

String substring method is used to get a part of String using the start and end index.
String had 2 substring methods.

1. String substring(int beginIndex) returns part of the String from the given start index till the end of the String.
If beginIndex is greater than the length of the String or is negative, then IndexOutOfBoundsException is thrown

2. String substring(int beginIndex,int endIndex) returns part of the String from the given start index till the end Index that is specified.
If beginIndex is greater than the length of the String or is negative or is greater than endIndex, then IndexOutOfBoundsException is thrown.
If endIndex is greater than the length of the String or is negative or is less then beginIndex, then IndexOutOfBoundsException is thrown.

String substring example

Output

Reference

1. String substring(int)- API

2. String substring(int,int)-API

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn