Facebook Page Twitter Page LinkedIn Page
× Java Tutorial Java Advanced Java Resources


The Java string 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 below example, 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

5