Programming
Java
String Conversion
CharSequence
Coding Tutorial

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:

java
String str = "Hello, World!";
CharSequence charSequence = str;  // Direct assignment

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 new CharSequence that 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 CharSequence accepts any implementation without changes to its code.
  • Performance: callers can pass whichever implementation suits them. For example, StringBuilder is 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

java
1public static int countLetters(CharSequence cs) {
2    int count = 0;
3    for (int i = 0; i < cs.length(); i++) {
4        if (Character.isLetter(cs.charAt(i))) {
5            count++;
6        }
7    }
8    return count;
9}
10
11public static void main(String[] args) {
12    String str = "Hello 123";
13    StringBuilder sb = new StringBuilder("Hello 123, StringBuilder!");
14    System.out.println(countLetters(str));         // Output: 5
15    System.out.println(countLetters(sb));          // Output: 18
16}

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

FeatureStringStringBuilderStringBuffer
Implements CharSequenceYesYesYes
MutableNoYesYes
Thread-safeYesNoYes
PerformanceHighHigherModerate

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.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms