How to convert a String to CharSequence?
Solution 1
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
.
Solution 2
Straight answer:
String s = "Hello World!";
// String => CharSequence conversion:
CharSequence cs = s; // String is already a CharSequence
CharSequence
is an interface, and the String
class implements CharSequence
.
Solution 3
CharSequence is an interface and String is its one of the implementations other than StringBuilder, StringBuffer and many other.
So, just as you use InterfaceName i = new ItsImplementation()
, you can use CharSequence cs = new String("string")
or simply CharSequence cs = "string";
Solution 4
You can use
CharSequence[] cs = String[] {"String to CharSequence"};
Solution 5
That's a good question! You may get into troubles if you invoke API that uses generics and want to assign or return that result with a different subtype of the generic type. Java 8 helps to transform:
List<String> input = new LinkedList<>(Arrays.asList("a", "b", "c"));
List<CharSequence> result;
// result = input; // <-- Type mismatch: cannot convert from List<String> to List<CharSequence>
result = input.stream().collect(Collectors.toList());
System.out.println(result);
Related videos on Youtube
![Daksh Gargas](https://i.stack.imgur.com/P8cIu.png?s=256&g=1)
Daksh Gargas
"That if statement had something else afterwards. (Pun intended)" I miss working on swift. Loves to play with 0s and 1s. Prefers using enums.
Updated on July 17, 2021Comments
-
Daksh Gargas almost 3 years
How to convert
String
toCharSequence
in Java?-
Jeff Scott Brown about 10 yearsThe question is kind of non sensical. Converting a String to a CharSequence is like converting a flute to a musical instrument. A String already is a CharSequence. The String class implements the CharSequence interface.
-
Raedwald over 9 yearsThe OP's confusion might stem from not knowing what it means to program to an interface
-
Kheldar over 9 years@JeffScottBrown the question actually makes sense, it's a legitimate wonder to anyone reading through the Android or Java doc and missing the detail that CharSequence is not a class. Your comment helped me, but if the question had not been worded the way it is, i wouldn't have found it and have spent more time looking for an answer than it's really worth ;)
-
BlackEye about 9 yearsI came to this question in the context of a groovy Problem:
Exception groovy.lang.MissingMethodException: No signature of method: static java.util.regex.Pattern.matcher() is applicable for argument types: (java.lang.String)
-
Vighnesh almost 7 yearsHere's how I'd advice @JeffScottBrown to reword his comment- "The String class implements (IS-A) the CharSequence interface so you can pass in a String in place of a CharSequence. It's like how you could refer to a flute as a musical instrument because a flute IS-A musical instrument"
-
-
gustafc almost 15 yearsExcept that you can't assign a
CharSequence
to aString
without an explicit cast. -
João Silva almost 15 yearsFixed the example, thanks. I meant the other way around, i.e., assign a
String
to aCharSequence
. -
Mugen over 13 yearsI'm confused by this code "CharSequence cs = "string"; ". How can we instantiate a CharSequence? Isn't that an Interface? (Sorry I'm still learning Java).
-
João Silva over 13 yearsYou are correct. It is indeed an interface. However, that code does not instantiate a
CharSequence
. It simply assigns an instance ofString
to aCharSequence
variable, and sinceString
implements theCharSequence
interface, the code works. -
trante over 10 yearsThis is not the answer. Question is String -> to -> CharSequence. Answer is reverse.
-
Alex A. over 10 yearsThis answers both. Poster first trivially resolves the String -> CharSequence problem by explaining that a String IS a CharSequence. Then poster answers how to go from CharSequence to String.
-
Jean-Rémy Revy almost 10 yearsWelcome to Stack Overflow Lucas. Please provide more explanation than a code snippet. It may be obvious for us, but help others letting them know why this code answer the question.
-
tishma about 7 yearsWrapping a string into an array doesn't help making a point that String is a CharSequence (and hence that array of CharSequence is array of String).
-
avez raj over 6 yearsCharSequence[] cs = new CharSequence[] {"String to CharSequence"};