Deep Dive Java CharSequence Interface


The CharSequence interface is used to provide uniform, read-only access to many different kinds of char sequences.

There are six methods in this interface.

  1. char charAt(int index)
  2. default IntStream chars()
  3. default IntStream codePoints()
  4. int length()
  5. CharSequence subSequence(int start, int end)
  6. String toString()


Let's take a look at each of them.

1. char charAt(int index)

    This method returns the char value at the specified index in the character sequence. The index parameter represents the position of the desired character, ranging from 0 to length() - 1. It provides a way to access individual characters within the sequence.

    Example:
    CharSequence charSequence = "Code2care";
    char charAtIndex = charSequence.charAt(2);
    System.out.println(charAtIndex);
    Output: d


2. default IntStream chars()

    The chars() method returns a stream of int values, zero-extending the char values from the character sequence. It was introduced in Java 8 as a default method.

    Example:
    CharSequence charSequence = "Code2care";
    IntStream intStream = charSequence.chars();
    intStream.forEach(System.out::println);
    Output:
    101
    50
    99
    97
    114
    101


3. default IntStream codePoints()

    The codePoints() method returns a stream of code point values from the character sequence.

    It handles surrogate pairs, combining them using Character.toCodePoint.

    The resulting stream provides access to Unicode code points. Similar to chars()

    This method is a default method introduced in Java 8.

    Example:
    CharSequence charSequence = "😊Code2care";
    IntStream codePointsStream = charSequence.codePoints();
    codePointsStream.forEach(codePoint -> System.out.println("Code Point: " + codePoint));
    Output:
    Code Point: 128522
    Code Point: 67
    Code Point: 111
    Code Point: 100
    Code Point: 101
    Code Point: 50
    Code Point: 99
    Code Point: 97
    Code Point: 114
    Code Point: 101


4. int length()

    The length() method returns the number of 16-bit characters in the character sequence.

    Example:
    CharSequence charSequence = "Code2care";
    int sequenceLength = charSequence.length();
    System.out.println("Length of the sequence: " + sequenceLength);
    Output: Length of the sequence: 7


5. CharSequence subSequence(int start, int end)

    It returns a CharSequence that is a subsequence of this sequence.

    Example:
    public static void main(String[] args) {
            CharSequence original = "Welcome to Code2care!";
            CharSequence subSequence = original.subSequence(11, 20);
            System.out.println("Subsequence: " + subSequence);
        }
    Output: Code2care


6. String toString()

    The toString() method returns a String containing the characters in the sequence.

    Example:
    public static void main(String[] args) {
            CharSequence charSequence = new StringBuilder("Welcome to ").append("Code2care");
            String resultString = charSequence.toString();
            System.out.println(resultString);
        }
    Output: Welcome to Code2care

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright Β© Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap