How to convert String to CharSequence in Java?
6 Answers
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.
7 Comments
CharSequence to a String without an explicit cast.String to a CharSequence.CharSequence. It simply assigns an instance of String to a CharSequence variable, and since String implements the CharSequence interface, the code works.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.
Comments
You can use
CharSequence[] cs = String[] {"String to CharSequence"};
3 Comments
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);
Comments
Attempting to provide some (possible) context for OP's question by posting my own trouble. I'm working in Scala, but the error messages I'm getting all reference Java types, and the error message reads a lot like the compiler complaining that CharSequence is not a String. I confirmed in the source code that String implements the CharSequence interface, but the error message draws attention to the difference between String and CharSequence while hiding the real source of the trouble:
scala> cols
res8: Iterable[String] = List(Item, a, b)
scala> val header = String.join(",", cols)
<console>:13: error: overloaded method value join with alternatives:
(x$1: CharSequence,x$2: java.lang.Iterable[_ <: CharSequence])String <and>
(x$1: CharSequence,x$2: CharSequence*)String
cannot be applied to (String, Iterable[String])
val header = String.join(",", cols)
I was able to fix this problem with the realization that the problem wasn't String / CharSequence, but rather a mismatch between java.lang.Iterable and Scala's built-in Iterable.
scala> val header = String.join(",", coll: _*)
header: String = Item,a,b
My particular problem can also be solved via the answers at Scala: join an iterable of strings
In summary, OP and others who come across similar problems should parse the error messages very closely and see what other type conversions might be involved.
1 Comment
String.replace method with argument types (Char), but it needs two arguments, so I added the empty String as the second argument (which I assumed was the default) and converted the first argument to String (since it needed to match either both Char or CharSequence. So, mine was another example of closely reading the argument type options vs. my own method call.
Exception groovy.lang.MissingMethodException: No signature of method: static java.util.regex.Pattern.matcher() is applicable for argument types: (java.lang.String)