How to convert a String to CharSequence in Java
Codemia Knowledge Hub ·
You do not need to convert anything. String implements the CharSequence interface, so every String already is a CharSequence and can be assigned or passed wherever one is expected:
Because the relationship is built into the String class itself, there is no method to call. StringBuilder and StringBuffer implement CharSequence too. That is why APIs that accept CharSequence, such as the regex methods of Pattern, work equally well with String, StringBuilder or StringBuffer.
What CharSequence is
CharSequence is an interface in the java.lang package that gives uniform, read-only access to a sequence of characters. Classes that implement it can be handled in a uniform manner, whatever their specific implementation. In effect, CharSequence works as a data type for character data in general, so code written against it is not tied to one particular class of character sequence.
Its key methods:
char charAt(int index): Returns the character at a specified index.int length(): Returns the length of the sequence.CharSequence subSequence(int start, int end): Returns a newCharSequencethat is a subsequence of this sequence.IntStream chars(): Returns an IntStream over the characters in the sequence.IntStream codePoints(): Returns an IntStream over the code points of the sequence.
Why accept CharSequence in your own methods
- API flexibility: a method that takes
CharSequenceaccepts any implementation without changes to its code. - Performance: callers can pass whichever implementation suits them. For example,
StringBuilderis often more memory-efficient for mutable character sequences. - Reusable code: the interface methods are enough to write generic code that works with any character data.
Example: one method for String and StringBuilder
The same countLetters method accepts the String and the StringBuilder without overloads or conversions, because both are passed as CharSequence.
String, StringBuilder and StringBuffer compared
| Feature | String | StringBuilder | StringBuffer |
Implements CharSequence | Yes | Yes | Yes |
| Mutable | No | Yes | Yes |
| Thread-safe | Yes | No | Yes |
| Performance | High | Higher | Moderate |
Knowing this relationship helps when designing APIs: accepting CharSequence instead of String makes a method more general and flexible at no cost to callers who already have a String.
Related reading
- How to convert an Array to a Set in Java
- How to convert an ArrayList containing Integers to primitive int array?
- How to convert an ArrayList to a strongly typed generic list without using a foreach?
- How to convert an Instant to a date format?
- How to convert an int array to String with toString method in Java
- How to convert an Iterator to a Stream?
- How to convert BigDecimal to Double in Java?
- How to convert CharSequence to String?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.