Java String - subSequence() Method
The Java subSequence() method returns a character sequence that is a subsequence of the given string.
Syntax
public CharSequence subSequence(int beginIndex, int endIndex)
Parameters
beginIndex |
specify the begin index (inclusive). |
endIndex |
specify the end index (exclusive). |
Return Value
Returns a character sequence that is a subsequence of the given string.
Exception
Throws IndexOutOfBoundsException, if beginIndex or endIndex is negative, if endIndex is greater than length(), or if beginIndex is greater than endIndex.
Example:
In the example below, subSequence() method is used to get the character sequence from the given string called MyString.
public class MyClass { public static void main(String[] args) { String MyString = "Hello World"; System.out.println(MyString.subSequence(0, 5)); } }
The output of the above code will be:
Hello
❮ Java String Methods