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
Facing issues? Have Questions? Post them here! I am happy to answer!
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
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
- Indent XML Formatting In Notepad++ - NotepadPlusPlus
- [fix] Spring Boot Data JPA - No identifier specified for entity - Java
- Fix: Minecraft EXCEPTION_ACCESS_VIOLATION Fatal Error Java Runtime - Java
- Android [SDK Manager] The system cannot find the path specified - Android-Studio
- How to refresh Safari on Mac (macOS) using keyboard shortcut - MacOS
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics - Java
- MySQL #6 - Error on delete of './my-database/db.opt' (Errcode: 13 - Permission denied) - MySQL
- Change the default login shell on macOS Ventura 13.0 - MacOS