How to convert a String to CharSequence?

BurningIce picture BurningIce · Sep 8, 2009 · Viewed 349k times · Source

How to convert String to CharSequence in Java?

Answer

João Silva picture João Silva · Sep 8, 2009

Since String IS-A CharSequence, you can pass a String wherever you need a CharSequence, or assign a String to a CharSequence:

CharSequence cs = "string";
String s = cs.toString();
foo(s); // prints "string"

public void foo(CharSequence cs) { 
  System.out.println(cs);
}

If you want to convert a CharSequence to a String, just use the toString method that must be implemented by every concrete implementation of CharSequence.

Hope it helps.