Java StringBuilder subSequence() Method

The StringBuilder.subSequence() method in Java is used to extract a subsequence of characters from a StringBuilder object.

Table of Contents

  1. Introduction
  2. subSequence Method Syntax
  3. Examples
    • Extracting a Subsequence
    • Using a Subsequence in Comparisons
  4. Real-World Use Case
  5. Conclusion

Introduction

The StringBuilder.subSequence() method is a member of the StringBuilder class in Java. It allows you to extract a specific range of characters as a CharSequence from the StringBuilder object. This method is useful when you need a part of the sequence without modifying the original StringBuilder.

subSequence() Method Syntax

The syntax for the subSequence method is as follows:

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

The method returns a CharSequence that starts at the specified start index and ends at the specified end index.

Examples

Extracting a Subsequence

The subSequence method can be used to extract a part of the character sequence from a StringBuilder.

Example

public class StringBuilderSubSequenceExample {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("Hello, World!");

        // Extract a subsequence from index 7 to 12
        CharSequence subSeq = sb.subSequence(7, 12);

        // Print the result
        System.out.println("Subsequence: " + subSeq);
    }
}

Output:

Subsequence: World

Using a Subsequence in Comparisons

You can use the subSequence method to extract parts of a StringBuilder for comparisons.

Example

public class StringBuilderSubSequenceExample {
    public static void main(String[] args) {
        StringBuilder sb1 = new StringBuilder("Hello, World!");
        StringBuilder sb2 = new StringBuilder("Hello, Java!");

        // Extract subsequences from both StringBuilders
        CharSequence subSeq1 = sb1.subSequence(7, 12);
        CharSequence subSeq2 = sb2.subSequence(7, 11);

        // Compare the subsequences
        boolean areEqual = subSeq1.equals(subSeq2);

        // Print the result
        System.out.println("Subsequence 1: " + subSeq1);
        System.out.println("Subsequence 2: " + subSeq2);
        System.out.println("Are the subsequences equal? " + areEqual);
    }
}

Output:

Subsequence 1: World
Subsequence 2: Java
Are the subsequences equal? false

Real-World Use Case

Example: Extracting a Substring for Validation

In a real-world scenario, you might need to extract and validate parts of a string, such as checking if a certain portion of an input string matches a required format.

Example Code

public class SubstringValidator {
    public static void main(String[] args) {
        StringBuilder input = new StringBuilder("User12345");

        // Extract the numeric part of the string
        CharSequence numericPart = input.subSequence(4, input.length());

        // Validate if the numeric part contains only digits
        boolean isValid = isNumeric(numericPart);

        // Print the validation result
        System.out.println("Numeric part: " + numericPart);
        System.out.println("Is the numeric part valid? " + isValid);
    }

    public static boolean isNumeric(CharSequence cs) {
        for (int i = 0; i < cs.length(); i++) {
            if (!Character.isDigit(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}

Output:

Numeric part: 12345
Is the numeric part valid? true

Conclusion

The StringBuilder.subSequence() method in Java is used for extracting a subsequence of characters from a StringBuilder object. By understanding how to use this method, you can efficiently manage and manipulate specific parts of a character sequence. Whether you need to extract a part of the sequence for further processing, perform comparisons, or validate substrings, the subSequence method provides a reliable solution for these tasks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top