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.
- char charAt(int index)
- default IntStream chars()
- default IntStream codePoints()
- int length()
- CharSequence subSequence(int start, int end)
- 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
Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!