Java : CharSequence with Examples

CharSequence (Java SE 22 & JDK 22) with Examples.
You will find code examples on most CharSequence methods.


Summary

A CharSequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences.

Class diagram

// Prints one character at a time.
public void print(CharSequence cs) {

    cs.codePoints().forEach(c -> {
        System.out.printf("%c,", c);
    });
    System.out.println();
}
final var str = "abcd";
print(str);

final var sb = new StringBuilder("xyz");
print(sb);

sb.append("ABC");
print(sb);

// Result
// ↓
//a,b,c,d,
//x,y,z,
//x,y,z,A,B,C,

Methods

char charAt (int index)

Returns the char value at the specified index.

final CharSequence cs = "abc";
//cs.charAt(-1); // IndexOutOfBoundsException
System.out.println(cs.charAt(0)); // a
System.out.println(cs.charAt(1)); // b
System.out.println(cs.charAt(2)); // c
//cs.charAt(3); // IndexOutOfBoundsException
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final CharSequence cs = surrogate + "ab";
System.out.println(cs); // "😊ab"

final var ret1 = cs.charAt(0);
System.out.println(ret1); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret1)); // d83d

final var ret2 = cs.charAt(1);
System.out.println(ret2); // ? <- Garbled characters.
System.out.println(Integer.toHexString(ret2)); // de0a

final var ret3 = cs.charAt(2);
System.out.println(ret3); // a

final var ret4 = cs.charAt(3);
System.out.println(ret4); // b

//cs.charAt(4); // IndexOutOfBoundsException

default IntStream chars ()

Returns a stream of int zero-extending the char values from this sequence.

final CharSequence cs = "abcd";

cs.chars().forEach(c -> {
    System.out.printf("%x : %c%n", c, c);
});

// Result
// ↓
// "61 : a"
// "62 : b"
// "63 : c"
// "64 : d"
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final CharSequence cs = "ab" + surrogate;
System.out.println(cs); // "ab😊"

cs.chars().forEach(c -> {
    System.out.printf("%x : %c%n", c, c);
});

// Result
// ↓
// "61 : a"
// "62 : b"
// "d83d : ?" <- Garbled characters.
// "de0a : ?" <- Garbled characters.

default IntStream codePoints ()

Returns a stream of code point values from this sequence.

final CharSequence cs = "abcd";

cs.codePoints().forEach(cp -> {
    System.out.printf("%x : %c%n", cp, cp);
});

// Result
// ↓
// "61 : a"
// "62 : b"
// "63 : c"
// "64 : d"
// An example with a surrogate code point.
final var surrogate = "\uD83D\uDE0A"; // "😊" is one character.
System.out.println(surrogate); // "😊"

final CharSequence cs = "ab" + surrogate;
System.out.println(cs); // "ab😊"

cs.codePoints().forEach(cp -> {
    System.out.printf("%x : %c%n", cp, cp);
});

// Result
// ↓
// "61 : a"
// "62 : b"
// "1f60a : 😊"

static int compare (CharSequence cs1, CharSequence cs2)

Compares two CharSequence instances lexicographically.

final CharSequence cs1 = "abcd";
final CharSequence cs2 = "xyz";
final CharSequence cs3 = "XYZ";

System.out.println(CharSequence.compare(cs1, cs2)); // -23
System.out.println(CharSequence.compare(cs1, cs3)); // 9
System.out.println(CharSequence.compare(cs2, cs3)); // 32
final CharSequence cs1 = "abcd";
final CharSequence cs2 = new StringBuilder("xyz");
final CharSequence cs3 = new StringBuilder("abcd");

System.out.println(CharSequence.compare(cs1, cs2)); // -23
System.out.println(CharSequence.compare(cs1, cs3)); // 0
System.out.println(CharSequence.compare(cs2, cs3)); // 23

default boolean isEmpty ()

Returns true if this character sequence is empty.

final CharSequence cs1 = "";
final CharSequence cs2 = "abcd";

System.out.println(cs1.isEmpty()); // true
System.out.println(cs2.isEmpty()); // false
final var sb = new StringBuilder();
final CharSequence cs = sb;

System.out.println(cs); // ""
System.out.println(cs.isEmpty()); // true

sb.append("xyz");
System.out.println(cs); // "xyz"
System.out.println(cs.isEmpty()); // false

int length ()

Returns the length of this character sequence.

final CharSequence cs1 = "";
final CharSequence cs2 = "abcd";

System.out.println(cs1.length()); // 0
System.out.println(cs2.length()); // 4
final var sb = new StringBuilder();
final CharSequence cs = sb;

System.out.println(cs); // ""
System.out.println(cs.length()); // 0

sb.append("xyz");
System.out.println(cs); // "xyz"
System.out.println(cs.length()); // 3

CharSequence subSequence (int start, int end)

Returns a CharSequence that is a subsequence of this sequence.

final CharSequence cs = "abcd";

System.out.println(cs.subSequence(0, 0)); // ""
System.out.println(cs.subSequence(0, 1)); // "a"
System.out.println(cs.subSequence(0, 2)); // "ab"
System.out.println(cs.subSequence(0, 3)); // "abc"
System.out.println(cs.subSequence(0, 4)); // "abcd"
//cs.subSequence(0, 5); // IndexOutOfBoundsException

System.out.println(cs.subSequence(0, 4)); // "abcd"
System.out.println(cs.subSequence(1, 4)); // "bcd"
System.out.println(cs.subSequence(2, 4)); // "cd"
System.out.println(cs.subSequence(3, 4)); // "d"
System.out.println(cs.subSequence(4, 4)); // ""

String toString ()

Returns a string containing the characters in this sequence in the same order as this sequence.

final CharSequence cs1 = "abcd";
final var str1 = cs1.toString();
System.out.println(str1); // "abcd"

final CharSequence cs2 = new StringBuilder("xyz");
final var str2 = cs2.toString();
System.out.println(str2); // "xyz"

Related posts

To top of page