Java String subSequence() Method

The String.subSequence() method in Java is used to retrieve a portion of a string as a CharSequence starting from a specified beginning index and ending at a specified end index. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. subSequence Method Syntax
  3. Examples
    • Extracting a SubSequence
    • Handling Edge Cases
    • Comparison with substring
  4. Conclusion

Introduction

The String.subSequence() method is a member of the String class in Java. It allows you to retrieve a portion of a string as a CharSequence from the specified start index to the end index (exclusive). This method is particularly useful when you need to work with a portion of a string without creating a new string object.

subSequence Method Syntax

The syntax for the subSequence method is as follows:

public CharSequence subSequence(int beginIndex, int endIndex)
  • beginIndex: The beginning index, inclusive.
  • endIndex: The ending index, exclusive.

Examples

Extracting a SubSequence

The subSequence method can be used to extract a portion of a string as a CharSequence.

Example

public class SubSequenceExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        CharSequence subSequence = message.subSequence(7, 12);

        System.out.println("Original message: " + message);
        System.out.println("SubSequence from index 7 to 12: " + subSequence);
    }
}

Output:

Original message: Hello, World!
SubSequence from index 7 to 12: World

Handling Edge Cases

The subSequence method can handle edge cases such as extracting subsequences at the bounds of the string. However, it will throw an IndexOutOfBoundsException if the specified indices are out of range.

Example

public class SubSequenceExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        // Valid range
        CharSequence subSequence1 = message.subSequence(0, 5);
        CharSequence subSequence2 = message.subSequence(7, message.length());

        System.out.println("SubSequence from index 0 to 5: " + subSequence1);
        System.out.println("SubSequence from index 7 to end: " + subSequence2);

        // Invalid range
        try {
            CharSequence invalidSubSequence = message.subSequence(7, 20);
            System.out.println("SubSequence from index 7 to 20: " + invalidSubSequence);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

Output:

SubSequence from index 0 to 5: Hello
SubSequence from index 7 to end: World!
Error: begin 7, end 20, length 13

Comparison with substring

The subSequence method returns a CharSequence, whereas the substring method returns a String. Both methods work similarly, but subSequence is more general and can be used in contexts where a CharSequence is required.

Example

public class SubSequenceExample {
    public static void main(String[] args) {
        String message = "Hello, World!";

        CharSequence subSequence = message.subSequence(7, 12);
        String substring = message.substring(7, 12);

        System.out.println("SubSequence from index 7 to 12: " + subSequence);
        System.out.println("Substring from index 7 to 12: " + substring);

        // Verify they are equal
        boolean areEqual = subSequence.toString().equals(substring);
        System.out.println("Are SubSequence and Substring equal? " + areEqual);
    }
}

Output:

SubSequence from index 7 to 12: World
Substring from index 7 to 12: World
Are SubSequence and Substring equal? true

Conclusion

The String.subSequence() method in Java is used for extracting portions of a string as a CharSequence. By understanding how to use this method, you can efficiently work with parts of strings without creating new string objects. Whether you are handling edge cases or comparing it with the substring method, the subSequence method provides a reliable solution for these tasks.

Comments